https://www.flickr.com/photos/dasprid/8148007408/
var mybeer = {
name: "Lagunitas",
type: "Indian Pale Ale",
barrels: 106000,
alcohol: 5.7,
manufacturer: {
name: "Lagunitas Brewing Company",
address: "1280 N McDowell Blvd, Petaluma, CA 94954, US"
},
tasting_notes: ["sweet", "fruit"]
};
db.beers.insert(mybeer);
// single condition
db.beers.find( { "name": "Lagunitas" } );
// AND condition
db.beers.find( { "type": "Indian Pale Ale", alcohol: { "$gte": 5 } } );
// OR condition
db.beers.find( { "$or": [
{ "type": "Indian Pale Ale" },
{ "alcohol": { "$gte": 5 } }
]});
var bulk = db.beers.initializeOrderedBulkOp();
// insert three new beers
bulk.insert( { name: "Lagunitas", alcohol: 5.7 } );
bulk.insert( { name: "Leffe", alcohol: 6.5 } );
bulk.insert( { name: "London Pride", alcohol: 4.7 } );
bulk.execute();
var bulk = db.beers.initializeUnorderedBulkOp();
// update one beer
bulk.find( { name: "Corona" } ).update( { $set: { alcohol: 4.3 } } );
// .. and delete another one
bulk.find( { name: "Chimay" } ).remove();
// execute everything as a single bulk operation
bulk.execute();
// expensive query with regex without anchor
db.articles.find( { "description": /August [0-9]+ 1969/ } ).maxTimeMS(30000)
// it also works with aggregation framework!
db.articles.aggregate([ {
"$match": {
"$text": {
"$search": "chien",
"$language": "fr"
}
}
}]).maxTimeMS(100);
// foreground by default
db.beers.ensureIndex( { name: 1} );
// background is an optional argument
db.beers.ensureIndex( { name: 1}, { background: true } );
db.articles.ensureIndex( { body: "text" } );
db.articles.insert(
{ body: "the quick brown fox jumped over the lazy dog" }
);
db.articles.find( { "$text" : { $search : "quickly" } } );
db.articles.aggregate([
{ "$match": { "$text": { $search: "bRoWN"} } }
]);
// search for a single word
db.articles.find( { "$text": { "$search": "coffee" } } );
// search for any of these words
db.articles.find( { "$text": { "$search": "bake coffee cake" } } );
// search for a phrase
db.articles.find( { "$text": { "$search": "\"coffee cake\"" } } );
// excluding some terms
db.articles.find( { "$text": { "$search": "bake coffee -cake" } } );
db.articles.find(
{ "$text": { "$search": "cake" } },
{ "score": { "$meta": "textScore" } }
).sort( { "score": { "$meta": "textScore" } } ).limit(3)
db.articles.find(
{ "$text": { "$search": "leche", $language: "es" } }
);
// using a cursor
db.beers.aggregate([
{ "$match": { barrels: { "$gte": 10000}} }
],
{ cursor: { batchSize: 1 } }
);
// output to a collection
db.beers.aggregate([
{ "$match" : { barrels : { "$gte": 10000}} },
{ "$out" : "my_output_collection" }
]);
db.beers.aggregate(
[
{ "$match": { barrels: { "$gte": 500 } } },
{ "$group": { "_id": "$type", count: { "$sum":1 } } }
],
{ explain: true }
);
{"stages" : [ // one entry per pipeline stage
{
"$cursor" : {
"query" : { "barrels" : { "$gte" : 500 } },
"fields" : { "type" : 1, "_id" : 0 },
"plan" : {
"cursor" : "BtreeCursor barrels_1",
"isMultiKey" : false,
"scanAndOrder" : false,
"indexBounds" : { "barrels" : [ [500, Infinity] ] }
}
}
],
"ok" : 1
}
db.beers.insert(
{ _id: 1, name: "Lagunitas", price: 10.99 }
);
// to increase the price by 20%
db.beers.update(
{ _id: 1 },
{ "$mul" : { price: 1.2 } }
);
db.beers.update(
{_id: 1},
{ "$bit": { mask: { and: NumberInt(10) } } }
);
db.beers.update(
{_id: 1},
{ "$bit": { mask: { or: NumberInt(10) } } }
);
db.beers.update(
{_id: 1},
{ "$bit": { mask: { xor: NumberInt(10) } } }
);
db.scores.insert(
{ _id: 1, low_score: 200, high_score: 400 }
);
db.scores.update(
{ _id: 1 },
{ "$min": { low_score: 250 } }
);
db.scores.update(
{ _id: 1 },
{ "$max": { high_score: 450 } }
);
// index creation
db.beers.ensureIndex( { barrels: 1 } );
db.beers.ensureIndex( { alcohol: 1 } );
// retrieval
db.beers.find( { barrels: { "$gte": 100 }, alcohol: { "$gte": 5.5 } } );
db.beers.find(
{ barrels: { "$gte": 300 } },
{ _id: -1, name: 1, barrels: 1}
).sort( { alcohol: -1 } );
db.runCommand( { planCacheListQueryShapes: "beers"});
{
"shapes" : [
{
"query" : { barrels: { "$gte": 300 } },
"sort" : { alcohol: -1 },
"projection" : { _id: -1, name: 1, barrels: 1}
}
]
}
db.runCommand({
"planCacheSetFilter": "beers",
"query" : { barrels: { "$gte": 300 } },
"sort" : { alcohol: -1 },
"projection" : { _id: -1, name: 1, barrels: 1}
"indexes": [
{ barrels: 1 },
{ alcohol: -1, barrels: 1 }
],
});
db.createRole({
role: "MMSMonitoringRole",
roles: ["clusterAdmin", "readAnyDatabase"]
});
db.createRole({
role: "MMSBackupRole",
roles: ["clusterAdmin", "readAnyDatabase", "userAdminAnyDatabase"]
});
db.addUser({
"user": "mms-monitoring",
"pwd": "abcd1234",
"roles": [
"MMSMonitoringRole"
]
});
db.addUser({
"user": "mms-backup",
"pwd": "efgh5678",
"roles": [
"MMSBackupRole"
]
});
db.createRole({
"role": "appUser",
"db": "myApp",
"privileges": [
{
"resource": { "db": "myApp" , "collection": "" },
"actions": [ "find", "dbStats", "collStats" ]
},
{
"resource": { "db": "myApp", "collection": "beers" },
"actions": [ "insert"]
}
]
};
mongod --dbpath data/db --auditDestination syslog
var auditEntry = { "atype" : "dropCollection", "ts" : { "$date" : "2014-04-08T16:48:34.333+1000" }, "local" : { "ip" : "127.0.0.1", "port" : 27017 }, "remote" : { "ip" : "127.0.0.1", "port" : 55771 }, "users" : [ { "user": "matias", "db": "test" } ], "param" : { "ns" : "test.beers" }, "result" : 0 };
var auditEntry = {
"atype" : "shutdown",
"ts" : { "$date" : "2014-04-08T16:54:24.373+1000" },
"local" : { "ip" : "127.0.0.1", "port" : 27017 },
"remote" : { "ip" : "127.0.0.1", "port" : 55771 },
"users" : [
{ "user": "matias", "db": "admin" }
],
"param" : {},
"result" : 0
};
https://www.youtube.com/watch?v=nSJiVXNsPHk&feature=youtu.be