如何通过Java在MongoDB中一次插入多个文档

32 java bulkinsert mongodb mongodb-java

我在我的应用程序中使用MongoDB,需要在MongoDB集合中插入多个文档.我使用的版本是1.6

我在这里看到了一个例子

http://docs.mongodb.org/manual/core/create/

在里面

批量插入多个文档 部分

作者传递数组的地方.

当我尝试相同的,但为什么它不允许,请告诉我如何一次插入多个文件?

package com;

import java.util.Date;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;

public class App {

    public static void main(String[] args) {
        try {
            MongoClient mongo = new MongoClient("localhost", 27017);
            DB db = mongo.getDB("at");
            DBCollection collection = db.getCollection("people");

            /*
             * BasicDBObject document = new BasicDBObject();
             * document.put("name", "mkyong"); document.put("age", 30);
             * document.put("createdDate", new Date()); table.insert(document);
             */

            String[] myStringArray = new String[] { "a", "b", "c" };

            collection.insert(myStringArray); // Compilation error at this line saying that "The method insert(DBObject...) in the type DBCollection is not applicable for the arguments (String[])"

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

请告诉我是什么方式,以便我可以通过java一次插入多个文档.

c.P*_*.u1 40

DBCollection.insert接受一个类型的参数DBObject,List<DBObject>或一个DBObjects 的数组,用于一次插入多个文档.你传入一个字符串数组.

您必须手动填充文档DBObject,将它们插入到一个List<DBObject>或一个DBObjects 数组中,最后插入insert它们.

DBObject document1 = new BasicDBObject();
document1.put("name", "Kiran");
document1.put("age", 20);

DBObject document2 = new BasicDBObject();
document2.put("name", "John");

List<DBObject> documents = new ArrayList<>();
documents.add(document1);
documents.add(document2);
collection.insert(documents);
Run Code Online (Sandbox Code Playgroud)

上面的代码段与您在MongoDB shell中发出的命令基本相同:

db.people.insert( [ {name: "Kiran", age: 20}, {name: "John"} ]);
Run Code Online (Sandbox Code Playgroud)

  • 我不确定其他方法,但使用Spring Data的[MongoOperations.insert()](http://static.springsource.org/spring-data/data-mongodb/docs/1.1.0.M1/api/),你可以摆脱`DBObject`创建部分(Spring为你做了). (2认同)

Vin*_*ins 20

在3.0之前,您可以在Java中使用以下代码

DB db = mongoClient.getDB("yourDB");
            DBCollection coll = db.getCollection("yourCollection");
            BulkWriteOperation builder = coll.initializeUnorderedBulkOperation();
            for(DBObject doc :yourList)
            {
                builder.insert(doc);
            }
            BulkWriteResult result = builder.execute();
            return result.isAcknowledged();
Run Code Online (Sandbox Code Playgroud)

如果您使用的是mongodb 3.0版,则可以使用

MongoDatabase database = mongoClient.getDatabase("yourDB");
            MongoCollection<Document> collection = database.getCollection("yourCollection");
            collection.insertMany(yourDocumentList);
Run Code Online (Sandbox Code Playgroud)


Sco*_*ott 17

从MongoDB 2.6和2.12版驱动程序开始,您现在也可以进行批量插入操作.在Java中,您可以使用BulkWriteOperation.使用此示例的示例可以是:

DBCollection coll = db.getCollection("user");
BulkWriteOperation bulk = coll.initializeUnorderedBulkOperation();
bulk.find(new BasicDBObject("z", 1)).upsert().update(new BasicDBObject("$inc", new BasicDBObject("y", -1)));
bulk.find(new BasicDBObject("z", 1)).upsert().update(new BasicDBObject("$inc", new BasicDBObject("y", -1)));
bulk.execute();
Run Code Online (Sandbox Code Playgroud)