olo
olo

Reputation: 5271

Mongodb database strcuture

I am new to MongoDB, I'm wondering if Mongodb is able to store the database like below?

{
    "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": {
        ...............
    }

}

if not, do I have to create more collections?

Upvotes: 0

Views: 52

Answers (2)

Ashish Aggarwal
Ashish Aggarwal

Reputation: 335

Yes absolutely, I can give you a rough idea and you should try then. You can use something like.

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"
}
)

Upvotes: 0

riteshtch
riteshtch

Reputation: 8769

Yes it can but you will have to use an array for info and weather field, here is an example:

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

To verify use db.coll.find().pretty()

Upvotes: 2

Related Questions