简体   繁体   中英

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. Is it possible to insert these array or without iterate but I want each one as separate document I am new to 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? 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);  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM