jackyesind
jackyesind

Reputation: 3373

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.

Upvotes: 1

Views: 480

Answers (1)

Mithun Satheesh
Mithun Satheesh

Reputation: 27845

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

Upvotes: 1

Related Questions