1
[
        {
        "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"
        }
]

我将其转换为 json 数组,然后迭代一个数组并将每个值插入 mongoDB。是否可以插入这些数组或不进行迭代,但我希望每个数组都作为单独的文档,我是 MongoDB 的新手。请提出任何建议。

4

1 回答 1

1

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? It works and create 5 separate docs in yourcollection

According to the mongo docs

for insert

db.collection.insert(document)

where document could be a document or array of documents to insert into the collection.

update

in java driver i tried the below and got working

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);  
于 2013-08-27T08:52:42.170 回答