How to insert a document in mongodb ?
db.collection.insert(<bson document>);
eg: db.collection.insert({"key":1000});
Java Example:
collection.insert(new BasicDBObject("key", 1000));
Bulk Insert:
db.collection.insert(<bson document 1>
<bson document 2>
)
eg:
db.collection.insert({"key":1000},{"key",2000});
Java Example:
collection.insert(new BasicDBObject("key", 1000),new BasicDBObject("key",2000));
MongoDB insert Performance using Java:
Code:
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
public class MongoInsertPerformance {
static int size = 10000;
public static void main(String[] args) throws UnknownHostException {
MongoClient mongoClient = new MongoClient("127.0.0.1");
DB db = mongoClient.getDB("mydb");
DBCollection collection = db.getCollection("test");
long startTime = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
collection.insert(new BasicDBObject("" + i, "" + i));
}
long endTime = System.currentTimeMillis();
System.out.println("Total Time taken in insert is "
+ (endTime - startTime));
// Bulk insert
collection.drop();
collection = db.getCollection("test");
startTime = System.currentTimeMillis();
List<DBObject> bulkInsertList = new ArrayList<DBObject>();
for (int i = 0; i < size; i++) {
bulkInsertList.add(new BasicDBObject("" + i, "" + i));
}
collection.insert(bulkInsertList);
endTime = System.currentTimeMillis();
System.out.println("Total Time taken in insert is "
+ (endTime - startTime));
}
}
Output with 10K :
Total Time taken in simple insert is 2019
Total Time taken in bulk insert is 192
Output with 100K :
Total Time taken in simple insert is 13661
Total Time taken in bulk insert is 1197
- Simple Insert - One Document at a Time
- Bulk Insert - Multiple Document at a Time.
Simple Insert :
eg: db.collection.insert({"key":1000});
Java Example:
collection.insert(new BasicDBObject("key", 1000));
Bulk Insert:
db.collection.insert(<bson document 1>
<bson document 2>
)
eg:
db.collection.insert({"key":1000},{"key",2000});
Java Example:
collection.insert(new BasicDBObject("key", 1000),new BasicDBObject("key",2000));
MongoDB insert Performance using Java:
Code:
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
public class MongoInsertPerformance {
static int size = 10000;
public static void main(String[] args) throws UnknownHostException {
MongoClient mongoClient = new MongoClient("127.0.0.1");
DB db = mongoClient.getDB("mydb");
DBCollection collection = db.getCollection("test");
long startTime = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
collection.insert(new BasicDBObject("" + i, "" + i));
}
long endTime = System.currentTimeMillis();
System.out.println("Total Time taken in insert is "
+ (endTime - startTime));
// Bulk insert
collection.drop();
collection = db.getCollection("test");
startTime = System.currentTimeMillis();
List<DBObject> bulkInsertList = new ArrayList<DBObject>();
for (int i = 0; i < size; i++) {
bulkInsertList.add(new BasicDBObject("" + i, "" + i));
}
collection.insert(bulkInsertList);
endTime = System.currentTimeMillis();
System.out.println("Total Time taken in insert is "
+ (endTime - startTime));
}
}
Output with 10K :
Total Time taken in simple insert is 2019
Total Time taken in bulk insert is 192
Output with 100K :
Total Time taken in simple insert is 13661
Total Time taken in bulk insert is 1197