Skip to content

Instantly share code, notes, and snippets.

@PhillipMwaniki
Created July 5, 2017 17:59
Show Gist options
  • Save PhillipMwaniki/6318e5a9e66781672779aef2adc7efb6 to your computer and use it in GitHub Desktop.
Save PhillipMwaniki/6318e5a9e66781672779aef2adc7efb6 to your computer and use it in GitHub Desktop.
package tech.caveman;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.ArrayList;
import java.util.List;
public class MongoThread implements Runnable{
public Document document;
public MongoThread(Document document) {
this.document = document;
}
@Override
public void run() {
try(MongoClient mongoClient = new MongoClient("127.0.0.1", 27017)){
//connect to db
MongoDatabase db = mongoClient.getDatabase("test");
System.out.println("Connected to Database successfully");
MongoCollection<Document> collection = db.getCollection("people_collection");
System.out.println("Collection created successfully");
List<Document> documents = new ArrayList<>();
documents.add(document);
collection.insertMany(documents);
System.out.println("Document added successfully");
}catch (Exception ex){
System.err.println( ex.getClass().getName() + ": " + ex.getMessage() );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment