| space, → | next slide |
| ← | previous slide |
| d | debug mode |
| ## <ret> | go to slide # |
| c | table of contents (vi) |
| f | toggle footer |
| r | reload slides |
| z | toggle help (this) |
$ mongo
> use blog
> db.posts.insert({
title: 'First post',
author: 'TomK32',
tags: ['metalab', 'hacking', 'mongodb'],
body: 'This is my first blogpost.',
published_at: new Date() })
// show all posts
> db.posts.find()
// Find by author
> db.posts.find({ author: 'TomK32' })
{
"_id" : ObjectId("4be28c83ab28e8473a69687f"),
"title" : "First post",
"author" : "TomK32",
"tags" : [ "metlab", "hacking", "mongodb"],
"body" : "This is my first blogpost.",
"published_at" : "Wed Mar 31 2010 20:31:44 GMT+0200 (CEST)"
}
// Find by value inside an Array, like tags
> db.posts.find({ tags: 'metalab' })
// not by TomK32 but tagged 'metalab'
> db.posts.find({ author: { $ne: 'TomK32' }, tags: 'metalab' })
// has no tags
> db.posts.find({tags: {$exists: false}})
// no future posts
> db.posts.find({published_at: {$lt: new Date()}})
> db.posts.update({_id: ObjectId('4be28c83ab28e8473a69687f')},
{ $push: { comments: { author: 'Sammy', body: 'Buy V1agra!!elf!' }}})
// Only get the comments
> db.posts.find({}, {comments:1})
{
"_id" : ObjectId("4be28c83ab28e8473a69687f"),
"comments" : [
{
"author" : "Sammy",
"body" : "Buy V1agra!!elf!"
}
]
}
> db.posts.find({'comments.author': 'Sammy' }, {comments: 1})
// Upsert
> db.posts.update( {'comments.author': 'Sammy'},
{$inc:{'comments.$.votes':1}}, false, true )
{
"_id" : ObjectId("4be28c83ab28e8473a69687f"),
"comments" : [
{
"author" : "Sammy",
"body" : "Buy V1agra!!elf!",
"votes" : 1
}
]
}
// Regexp!
> db.posts.find({'comments.body': /Buy/ })