如何通过Java在MongoDB中一次插入多个文档 [英] How to insert multiple documents at once in MongoDB through Java
问题描述
我在我的应用程序中使用MongoDB,需要在MongoDB集合中插入多个文档。
我使用的版本是1.6
我在这里看到了一个例子
http://docs.mongodb.org/manual/core/create/
$ b
批量插入多个文档部分
其中作者正在传递一个数组来执行此操作。
当我尝试相同但为什么不允许时,请告诉我如何在多个文档中插入多个文档一次??
package com;
import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
公共类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(年龄,30);
* document.put(createdDate,new Date()); table.insert(文件);
* /
String [] myStringArray = new String [] {a,b,c};
collection.insert(myStringArray); //此行的编译错误说DBCollection类型中的方法插入(DBObject ...)不适用于参数(String [])
} catch(例外e){
e.printStackTrace();
}
}
}
请让我知道是什么方式,以便我可以通过java一次插入多个文档。
DBCollection.insert
接受类型 DBObject
, List< DBObject>
的参数或者一个 DBObject
的数组,用于一次插入多个文档。您传入的是字符串数组。
您必须手动填充文档( DBObject
s),将它们插入到列出< DBObject>
或 DBObject
的数组,最终 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);
上面的代码段与您在MongoDB shell中发出的命令基本相同:
db.people.insert([{name:Kiran,年龄:20},{name:John}]);
I am using MongoDB in my application and was needed to insert multiple documents inside a MongoDB collection . The version I am using is of 1.6
I saw an example here
http://docs.mongodb.org/manual/core/create/
in the
Bulk Insert Multiple Documents Section
Where the author was passing an array to do this .
When I tried the same , but why it isn't allowing , and please tell me how can I insert multiple documents at once ??
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();
}
}
}
Please let me know what is the way so that I can insert multiple documents at once through java .
DBCollection.insert
accepts a parameter of type DBObject
, List<DBObject>
or an array of DBObject
s for inserting multiple documents at once. You are passing in a string array.
You must manually populate documents(DBObject
s), insert them to a List<DBObject>
or an array of DBObject
s and eventually insert
them.
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);
The above snippet is essentially the same as the command you would issue in the MongoDB shell:
db.people.insert( [ {name: "Kiran", age: 20}, {name: "John"} ]);
这篇关于如何通过Java在MongoDB中一次插入多个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!