Simon
Simon

Reputation: 871

Not all documents are inserted in MongoDB when using the async-driver for Java

I was experimenting with the mongodb-async driver(http://mongodb.github.io/mongo-java-driver/3.0/driver-async/) and noticed odd behaviour. I reproduced the weird behaviour in underlying code:

import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.MongoClient;
import com.mongodb.async.client.MongoClients;
import com.mongodb.async.client.MongoCollection;
import com.mongodb.async.client.MongoDatabase;
import org.bson.Document;


public class main {
    public static void main(String [] args)
    {
        MongoClient mongoClient = MongoClients.create();
        MongoDatabase database = mongoClient.getDatabase("mongotest");
        MongoCollection<Document> collection = database.getCollection("coll");


        for(Integer i = 0; i < 100000; i++) {
            Document doc = new Document("name"+ i.toString(), "TESTING");
            collection.insertOne(doc, new SingleResultCallback<Void>() {
                public void onResult(final Void result, final Throwable t) {
                    System.out.println("Inserted!");
                }
            });
        }


        while(true){
        }
    }
}

I would expect this code to insert 100.000 documents into the collection 'coll' of the mongo-database called "mongotest". However, when I check the number of elements after running this code, thousands of documents are missing.

When running this statement in the mongodb-shell

db.getCollection("coll").count()

I get 93062 as a result. This number varies for each run but never gets up to 100.000. Can anyone explain why not all objects are properly stored as documents in the MongoDB when I use this code? We tested this on 3 different machines and every machine exposed the same behaviour.

I have the feeling it is a driver-related issue because following up on this I wrote a similar experiment using node.js:

var express = require('express');
var MongoClient = require('mongodb').MongoClient;
var app = express();
var url = 'mongodb://localhost:27017/mongotest';

MongoClient.connect(url, function (err, db) {
  for (var i = 0; i < 100000; i++) {
    var name = "name" + i;
    db.collection("coll").insertOne({
      name: name
    },function(err,results) {
      if(err==null) {
        console.log("Sweet");
      }
    });
  }
});


module.exports = app;

This code took longer to run in comparison to the java-code but when the code finishes, 100.000 documents are sitting in the collection as expected.

Can anyone explain why this is not the case with the java-example, and possibly provide a solution?

Upvotes: 4

Views: 704

Answers (1)

zydcom
zydcom

Reputation: 518

When did you run db.getCollection("coll").count() to check the insert result?

Maybe the insertion process has not finished when you check the result.


2016-02-19 15:00 edit

I did the same test, and had the same result.

but when I change the following line

 Document doc = new Document("name"+ i.toString(), "TESTING"); 

to

 Document doc = new Document("_id", "name"+ i.toString());

It inserted exactly 100000 docoments.

Upvotes: 0

Related Questions