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

I experimented with the mongodb-async driver ( http://mongodb.imtqy.com/mongo-java-driver/3.0/driver-async/ ) and noticed strange behavior. I reproduced the strange behavior in the base 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 inject 100,000 documents into the 'coll' collection of 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()

93062. , 100 000. - , MongoDB, ? , .

, , , , 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;

java-, , 100.000 , .

- , java- , , ?

+4
1

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

, , .


2016-02-19 15:00

.

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

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

100000 .

0

Source: https://habr.com/ru/post/1629215/


All Articles