Mongodb将值列表作为单独的文档插入

时间:2013-08-27 08:31:55

标签: java mongodb

[
        {
        "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的新手。请提出任何建议。

1 个答案:

答案 0 :(得分:1)

你试过

吗?
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"
    }
]);
从您的mongo控制台

?它可以在yourcollection

中创建并创建5个单独的文档

根据mongo docs

用于插入

db.collection.insert(document)

其中document可以是要插入集合的文档或文档数组。

更新

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