[英]Mongo DB insert list of values as separate document
[
{
"id":"1",
"lat": "23.053",
"long": "72.629",
"location": "ABC",
"address": "DEF",
"city": "Ahmedabad",
"state": "Gujrat",
"phonenumber": "1234567"
},
{
"id":"2",
"lat": "23.053",
"long": "72.629",
"location": "ABC",
"address": "DEF",
"city": "Ahmedabad",
"state": "Gujrat",
"phonenumber": "1234567"
},
{
"id":"3",
"lat": "23.053",
"long": "72.629",
"location": "ABC",
"address": "DEF",
"city": "Ahmedabad",
"state": "Gujrat",
"phonenumber": "1234567"
},
{
"id":"4",
"lat": "23.053",
"long": "72.629",
"location": "ABC",
"address": "DEF",
"city": "Ahmedabad",
"state": "Gujrat",
"phonenumber": "1234567"
},
{
"id":"5",
"lat": "23.053",
"long": "72.629",
"location": "ABC",
"address": "DEF",
"city": "Ahmedabad",
"state": "Gujrat",
"phonenumber": "1234567"
}
]
I converted that into json array and I iterate an array and insert each value to mongoDB. 我将其转换为json数组,并迭代了一个数组,并将每个值插入到mongoDB中。 Is it possible to insert these array or without iterate but I want each one as separate document I am new to MongoDB.
是否可以插入这些数组或不进行迭代,但是我希望将每个数组作为单独的文件使用,所以我是MongoDB的新手。 Please give any suggestions.
请提出任何建议。
did you try 你试过了吗
db.yourcollection.insert([
{
"id":"1",
"lat": "23.053",
"long": "72.629",
"location": "ABC",
"address": "DEF",
"city": "Ahmedabad",
"state": "Gujrat",
"phonenumber": "1234567"
},
{
"id":"2",
"lat": "23.053","long": "72.629",
"location": "ABC",
"address": "DEF",
"city": "Ahmedabad",
"state": "Gujrat",
"phonenumber": "1234567"
},
{
"id":"3",
"lat": "23.053",
"long": "72.629",
"location": "ABC",
"address": "DEF",
"city": "Ahmedabad",
"state": "Gujrat",
"phonenumber": "1234567"
},
{
"id":"4",
"lat": "23.053",
"long": "72.629",
"location": "ABC",
"address": "DEF",
"city": "Ahmedabad",
"state": "Gujrat",
"phonenumber": "1234567"
},
{
"id":"5",
"lat": "23.053",
"long": "72.629",
"location": "ABC",
"address": "DEF",
"city": "Ahmedabad",
"state": "Gujrat",
"phonenumber": "1234567"
}
]);
from your mongo console? 从您的mongo控制台? It works and create 5 separate docs in
yourcollection
它可以在您的
yourcollection
创建5个单独的文档
According to the mongo docs 根据mongo docs
for insert 用于插入
db.collection.insert(document)
where document
could be a document or array of documents to insert into the collection. 其中
document
可以是要插入到集合中的一个文档或一系列文档。
in java driver i tried the below and got working 在Java驱动程序中,我尝试了以下操作并开始工作
try {
this.mongoClient = new MongoClient( "localhost" , 27017 );
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.db = mongoClient.getDB( "java" );
this.coll = db.getCollection("sample");
BasicDBObject[] doc = {new BasicDBObject("name", "MongoDB1"),new BasicDBObject("name", "MongoDB2"),new BasicDBObject("name", "MongoDB3")};
this.coll.insert(doc);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.