|
|
(from: MongoDB The Definitive Guide, O'Reilly)
RDBMS | Mongo |
---|---|
Table | Collection |
Row(s) | BSON Document |
Column | BSON Field |
Index | Index |
Join | Embedded Document |
Partition | Shard |
Partition Key | Shard Key |
var book = {
title: 'MongoDB: The Definitive Guide',
authors: [
{ lastName: 'Chodorow', firstName: 'Kristina' },
{ lastName: 'Dirolf', firstName: 'Michael' }
],
tags: ['NoSQL', 'Database', 'BigData', 'Programming'],
pages: 195,
published: 2010
};
db.books.save(book);
// primary key '_id'
// generated by client driver
// e.g. 4fba97070f318c1e73763350
book._id;
db.books.find(); // find all
// get count of all docs
db.books.count();
db.books.find().count();
// find by primary key (_id)
db.books.findOne({
_id: ObjectId("4fba97190f318c1e73763353")
});
db.books.find({ title: 'JavaScript Patterns' });
db.books.find({ title: /^MongoDB/ });
db.books.find({ title: /^MongoDB/, // and
pages: {$gt: 200} });
db.books.find({
'authors.lastName': 'Katz'});
db.books.find({
'authors.lastName':
{$in: ['Katz', 'McCaw']} });
db.books.find({
$or: [
{'authors.lastName': 'Katz'},
{'authors.lastName': 'McCaw'}
]});
db.books.find({authors: {$size:2} });
db.books.find(
{ tags: 'Programming' });
db.books.find({
tags: {$all: ['Programming',
'JavaScript']}
});
db.books.
find({/* all */},
{title: 1, pages: 1}).
sort({title: 1}).
limit(4);
db.books.update({title: /Good Parts/},
{$inc: {pages: 3}});
db.books.update({title: /in Action/},
{$set: {publisher: 'Manning'}},
false, true);
db.books.update({},
{$addToSet: {tags: 'Programming'}},
false, true);
db.books.remove({_id: mybook._id});
db.books.remove({tags: 'Cooking'});
db.books.remove(); // truncate col
db.books.remove({/* ... */}, {$atomic:true});
// blocking!
db.runCommand({count: 'books',
query: {published: 2012}});
db.runCommand({distinct: 'books', key:'tags'});
db.runCommand({group: {
ns: 'books',
key: { published: true },
$reduce: function (obj, prev) {
prev.pages += obj.pages;
},
initial: { pages: 0 }
}});
db.runCommand({ dropDatabase: 1 });
db.runCommand({ getLastError: 1 });
db.runCommand({ serverStatus: 1 });
db.runCommand({ shutdown: 1 });
GROUP BY
in SQL
db.runCommand({ mapreduce: 'collection_name',
map: my_map_function,
reduce: my_reduce_function /*, ...*/});
{ "title": "MongoDB in Action",
"authors": [
{ "lastName": "Banker",
"firstName": "Kyle" }
],
"tags": [ "NoSQL", "Database", "Programming" ],
"pages": 311
}
this
)
var map = function () {
var doc = this;
doc.tags.forEach(function (tag) {
emit(tag, { pages: doc.pages });
});
};
var reduce = function (key, values) {
var result = { pages: 0 };
values.forEach(function (value) {
result.pages += value.pages;
});
return result;
};
var map = function () {
var doc = this;
doc.tags.forEach(function (tag) {
emit(tag, { pages: doc.pages });
});
}, reduce = function (key, values) {
var result = { pages: 0 };
values.forEach(function (value) {
result.pages += value.pages;
});
return result;
};
db.books.mapReduce(map, reduce, {out: {inline: 1}});
db.books.ensureIndex({"title": 1}, {unique: true});
db.books.ensureIndex({"authors.lastName": 1});
db.books.ensureIndex({"tags": 1});
db.books.getIndexes();
db.books.dropIndex('title_1');
mongoexport -d test -c books > mongo.books.txt
mongo test --eval "db.books.remove()"
mongoimport -d test -c books --file books.txt
mongoexport -d test -c books --jsonArray > books.json
mongoimport -d test -c books --jsonArray < books.json
// my mail address
'hgoebl+goebl.com'.replace('+', '@')
|
|