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

如何通过Java在MongoDB中一次插入多个文档,java,mongodb,bulkinsert,mongodb-java,Java,Mongodb,Bulkinsert,Mongodb Java,我在应用程序中使用MongoDB,需要在MongoDB集合中插入多个文档。 我正在使用的版本是1.6 我在这里看到了一个例子 在 批量插入多个文档部分 作者正在传递一个数组来执行此操作 当我尝试同样的方法时,但为什么它不允许,请告诉我如何一次插入多个文档 package com; import java.util.Date; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBColle

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

我在这里看到了一个例子

批量插入多个文档部分

作者正在传递一个数组来执行此操作

当我尝试同样的方法时,但为什么它不允许,请告诉我如何一次插入多个文档

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();
        }

    }

}

请告诉我如何通过java一次插入多个文档。

DBCollection.insert
接受类型为
DBObject
List
的参数或一组
DBObject
的数组,用于一次插入多个文档。您正在传入一个字符串数组

您必须手动填充文档(
DBObject
s),将它们插入到
列表或
DBObject
s数组中,并最终插入它们

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

对于MongoDB 2.6和2.12版本的驱动程序,您现在还可以执行以下操作。在Java中,您可以使用。这方面的一个例子是:

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();

在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();
如果您使用的是mongodb 3.0版,则可以使用

MongoDatabase database = mongoClient.getDatabase("yourDB");
            MongoCollection<Document> collection = database.getCollection("yourCollection");
            collection.insertMany(yourDocumentList);
MongoDatabase=mongoClient.getDatabase(“yourDB”);
MongoCollection collection=database.getCollection(“yourCollection”);
collection.insertMany(您的文档列表);

您的插入记录格式(如MongoDB中的插入记录格式)将从任何源退出查询 例如

它是mongodb 3.0

FindIterable<Document> resulutlist = collection.find(query);            
List docList = new ArrayList();
for (Document document : resulutlist) {
    docList.add(document);
}

if(!docList.isEmpty()){
    collectionCube.insertMany(docList);
}   
findTerrable ResultList=collection.find(查询);
List docList=new ArrayList();
用于(文档:ResultList){
docList.add(文件);
}
如果(!docList.isEmpty()){
collectionCube.insertMany(文档列表);
}   
创建文档 在MongoDB中创建文档有两个主要命令:

  • insertOne()
  • insertMany()
还有其他方法,如
Update
命令。我们称这些行动为“升级”。当没有与用于标识文档的选择器相匹配的文档时,会发生Upserts

尽管MongoDB自己插入ID,但我们也可以通过在
insert…()
函数中指定
\u ID
参数手动插入自定义ID

要插入多个文档,我们可以使用
insertMany()
——它将文档数组作为参数。执行时,它为数组中的每个文档返回多个
id
s。要删除集合,请使用
drop()
命令。有时,在执行批量插入时,我们可能会插入重复的值。具体来说,如果我们尝试插入重复的
\u id
s,我们将得到
重复键错误:

db.startup.insertMany( [ {_id:"id1", name:"Uber"}, {_id:"id2", name:"Airbnb"}, {_id:"id1", name:"Uber"}, ] );
谢谢,有没有其他方法可以做到这一点,我想创建太多的DBObject是有可能的,因为我需要创建将近100个。那么有没有其他方法可以做到这一点呢?我不确定其他方法,但是使用Spring数据,你可以去掉
DBObject
创建部分(Spring为你做).我们如何通过Spring Data Mongo API做到这一点?谢谢,Deepa你可以在另一个帖子里看看我的回复。。如果执行此操作会给出“message='E11000 duplicate key error collection:”,该怎么办。如何解决这个问题?
FindIterable<Document> resulutlist = collection.find(query);            
List docList = new ArrayList();
for (Document document : resulutlist) {
    docList.add(document);
}

if(!docList.isEmpty()){
    collectionCube.insertMany(docList);
}   
db.startup.insertMany( [ {_id:"id1", name:"Uber"}, {_id:"id2", name:"Airbnb"}, {_id:"id1", name:"Uber"}, ] ); db.startup.insertMany( [ {_id:"id1", name:"Uber"}, {_id:"id2", name:"Airbnb"}, {_id:"id1", name:"Airbnb"}, ], {ordered: false} );