Mongodb数据库strcuture

时间:2016-04-28 05:02:33

标签: mongodb

我是MongoDB的新手,我想知道Mongodb是否能够像下面那样存储数据库?

{
    "settings": {
        "system": "windows",
        "date": "mm-dd-yyyy",
        "time": "HH-MM-SS"
    },

    "info": {

        { "_id" : "99921", "city" : "CRAIG", "loc" : [ -133.117081, 55.47317 ], "pop" : 1398, "state" : "AK" },
        { "_id" : "99922", "city" : "HYDABURG", "loc" : [ -132.633175, 55.137406 ], "pop" : 891, "state" : "AK" },
        { "_id" : "99923", "city" : "HYDER", "loc" : [ -130.124915, 55.925867 ], "pop" : 116, "state" : "AK" },
        { "_id" : "99925", "city" : "KLAWOCK", "loc" : [ -133.055503, 55.552611 ], "pop" : 851, "state" : "AK" },
        { "_id" : "99926", "city" : "METLAKATLA", "loc" : [ -131.579001, 55.121491 ], "pop" : 1469, "state" : "AK" },
        { "_id" : "99927", "city" : "POINT BAKER", "loc" : [ -133.376372, 56.307858 ], "pop" : 426, "state" : "AK" },
        { "_id" : "99929", "city" : "WRANGELL", "loc" : [ -132.352918, 56.433524 ], "pop" : 2573, "state" : "AK" },
        { "_id" : "99950", "city" : "KETCHIKAN", "loc" : [ -133.18479, 55.942471 ], "pop" : 422, "state" : "AK" }
    },

    "Weather": {
        ...............
    }

}

如果没有,我是否必须创建更多集合?

2 个答案:

答案 0 :(得分:2)

是的它可以,但你必须使用infoweather字段的数组,这是一个例子:

> use temp                                                                                                                             
switched to db temp                                                                                                                    
> db.coll.insert({                                                                                                                     
...     "settings": {                                                                                                                  
...         "system": "windows",                                                                                                       
...         "date": "mm-dd-yyyy",                                                                                                      
...         "time": "HH-MM-SS"                                                                                                         
...     },                                                                                                                             
...                                                                                                                                    
...     "info": [                                                                                                                      
...                                                                                                                                    
...         { "_id" : "99921", "city" : "CRAIG", "loc" : [ -133.117081, 55.47317 ], "pop" : 1398, "state" : "AK" },                    
...         { "_id" : "99922", "city" : "HYDABURG", "loc" : [ -132.633175, 55.137406 ], "pop" : 891, "state" : "AK" },                 
...         { "_id" : "99923", "city" : "HYDER", "loc" : [ -130.124915, 55.925867 ], "pop" : 116, "state" : "AK" },                    
...         { "_id" : "99925", "city" : "KLAWOCK", "loc" : [ -133.055503, 55.552611 ], "pop" : 851, "state" : "AK" },                  
...         { "_id" : "99926", "city" : "METLAKATLA", "loc" : [ -131.579001, 55.121491 ], "pop" : 1469, "state" : "AK" },              
...         { "_id" : "99927", "city" : "POINT BAKER", "loc" : [ -133.376372, 56.307858 ], "pop" : 426, "state" : "AK" },              
...         { "_id" : "99929", "city" : "WRANGELL", "loc" : [ -132.352918, 56.433524 ], "pop" : 2573, "state" : "AK" },                
...         { "_id" : "99950", "city" : "KETCHIKAN", "loc" : [ -133.18479, 55.942471 ], "pop" : 422, "state" : "AK" }                  
...     ],                                                                                                                             
...                                                                                                                                    
...     "Weather": [                                                                                                                   
... {"_id":"99921", "weather": "cloudy"},                                                                                              
... {"_id":"99922", "weather": "sunny"},                                                                                               
...     ]                                                                                                                              
... })                                                                                                                                 
WriteResult({ "nInserted" : 1 }) 

验证使用db.coll.find().pretty()

答案 1 :(得分:0)

是的,我可以给你一个粗略的想法,你应该尝试一下。你可以使用类似的东西。

db.collection_name.insert(
{
 settings: {
    system: "windows",
    date: "mm-dd-yyyy",
    time: "HH-MM-SS"
 },
 info: [ 
 {_id : "val", city : "val", loc : "val", pop : "val", state : "val"}, 
 {_id : "val", city : "val", loc : "val", pop : "val", state : "val"}
],
 Weather: "val"
}
)