Created
July 13, 2014 08:56
-
-
Save mh-github/2e8dd8eb4a384429cefc to your computer and use it in GitHub Desktop.
Inserting a million records into MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.net.UnknownHostException; | |
| import com.mongodb.Mongo; | |
| import com.mongodb.DB; | |
| import com.mongodb.DBCollection; | |
| import com.mongodb.BasicDBObject; | |
| import com.mongodb.DBCursor; | |
| import com.mongodb.MongoException; | |
| public class Mongo_Insert_Collection | |
| { | |
| public static void main(String[] args) { | |
| try { | |
| Mongo mongo = new Mongo("localhost", 27017); | |
| DB db = mongo.getDB("mh-db"); | |
| DBCollection collection = db.getCollection("mhColl"); | |
| BasicDBObject document; | |
| System.out.println("Inserting a million records"); | |
| long time1 = System.currentTimeMillis(); | |
| for (int i = 0; i < 1000000; i++) { | |
| document = new BasicDBObject(); | |
| document.append("age"+i+1, i+1); | |
| collection.insert(document); | |
| } | |
| long time2 = System.currentTimeMillis(); | |
| System.out.println("------------------"); | |
| System.out.println("Insert took " + (time2 - time1) + " ms"); | |
| } | |
| catch (UnknownHostException e) { | |
| e.printStackTrace(); | |
| } | |
| catch (MongoException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment