Hardy Erlinger
hardy.erlinger@netspectrum.de
www.netspectrum.de
NoSQL: “Not only SQL”
Relational DB | MongoDB |
---|---|
Database server | MongoDB instance |
Database | Database |
Table | Collection |
Row | Document |
Source: MongoDB Documentation
_id
for a document, the server adds it automatically
(type: ObjectId)
{
"_id" : ObjectId("54cce0151e98e91368b54a17"),
"productDetails" : {
"description" : "Lorem ipsum ...",
"productType" : "A"
},
//[...]
}
Source: MongoDB Documentation
Source: MongoDB Documentation
Source: MongoDB Documentation
Source: MongoDB Documentation
_id
is indexed automaticallydb.mycoll.ensureIndex({fieldName:1})
db.mycoll.find({...}).explain()
Source: MongoDB Documentation
Source: MongoDB Documentation
It depends ...
Get states with populations above 10 million
// sample doc from the "zipcodes" collection
{
"_id": "10280",
"city": "NEW YORK",
"state": "NY",
"pop": 5574,
"loc": [
-74.016323,
40.710537
]
}
// Get states with populations above 10 million, sort by population (desc)
db.zipcodes.aggregate(
{ $group : { _id : "$state", totalPop : { $sum : "$pop" } } },
{ $match : { totalPop : { $gte : 10*1000*1000 } } },
{ $sort : { totalPop: -1 } }
)
{
"result" : [
{
"_id" : "CA",
"totalPop" : 29760021
},
{
"_id" : "NY",
"totalPop" : 17990455
},
{
"_id" : "TX",
"totalPop" : 16986510
},
{
"_id" : "FL",
"totalPop" : 12937926
},
{
"_id" : "PA",
"totalPop" : 11881643
},
{
"_id" : "IL",
"totalPop" : 11430602
},
{
"_id" : "OH",
"totalPop" : 10847115
}
],
"ok" : 1
}
Source: MongoDB Documentation
Source: MongoDB Documentation