This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"forename": "Peter" | |
, "surname": "Membrey" | |
, "phone_numbers": [ | |
"+852 1234 5678" | |
, "+44 1234 565 555" | |
] | |
, "emails": [ | |
"[email protected]" | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> use store | |
switched to db store | |
> zoloo = {username: 'Zoloo', created_at: new Date('10/05/2011'), password: 'secret', last_visited_at: new Date('10/05/2011')} | |
{ | |
"username" : "Zoloo", | |
"created_at" : ISODate("2011-10-04T16:00:00Z"), | |
"password" : "secret", | |
"last_visited_at" : ISODate("2011-10-04T16:00:00Z") | |
} | |
> db.users.insert(zoloo) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> zoloo = {username: 'Zoloo', created_at: new Date('10/05/2011'), password: 'secret', last_visited_at: new Date('10/05/2011')} | |
{ | |
"username" : "Zoloo", | |
"created_at" : ISODate("2011-10-04T16:00:00Z"), | |
"password" : "secret", | |
"last_visited_at" : ISODate("2011-10-04T16:00:00Z") | |
} | |
> db.users.insert(zoloo) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.users.findOne() | |
{ | |
"_id" : ObjectId("4e8c2a00de570598c220878b"), | |
"username" : "Zoloo", | |
"created_at" : ISODate("2011-10-04T16:00:00Z"), | |
"password" : "secret", | |
"last_visited_at" : ISODate("2011-10-04T16:00:00Z") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.users.dataSize | |
function () { | |
return this.stats().size; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.users.insert({username: 'Otgoo', password: 'foobar'}) | |
> db.users.insert({username: 'Unuruu', password: 'barfoo', created_at: Date('10/05/2011')}) | |
> db.users.insert({username: 'Tuvhsuu', created_at: null}) | |
> db.users.find() | |
{ "_id" : ObjectId("4e8c2a00de570598c220878b"), "username" : "Zoloo", "created_at" : ISODate("2011-10-04T16:00:00Z"), "password" : "secret", "last_visited_at" : ISODate("2011-10-04T16:00:00Z") } | |
{ "_id" : ObjectId("4e8d02690ec65305be4f6566"), "username" : "Darmaa", "created_at" : "Thu Oct 06 2011 09:20:33 GMT+0800 (ULAT)" } | |
{ "_id" : ObjectId("4e8d0d420ec65305be4f6567"), "username" : "Otgoo", "password" : "foobar" } | |
{ "_id" : ObjectId("4e8d0d800ec65305be4f6568"), "username" : "Unuruu", "password" : "barfoo", "created_at" : "Thu Oct 06 2011 10:08:00 GMT+0800 (ULAT)" } | |
{ "_id" : ObjectId("4e8d0d9f0ec65305be4f6569"), "username" : "Tuvhsuu", "created_at" : null } | |
> db.users.find({created_at: null}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.users.find({created_at: null}) | |
{ "_id" : ObjectId("4e8d0d420ec65305be4f6567"), "username" : "Otgoo", "password" : "foobar" } | |
{ "_id" : ObjectId("4e8d0d9f0ec65305be4f6569"), "username" : "Tuvhsuu", "created_at" : null } | |
> db.users.find({created_at: {$type: 10}}) | |
{ "_id" : ObjectId("4e8d0d9f0ec65305be4f6569"), "username" : "Tuvhsuu", "created_at" : null } | |
> db.users.find({created_at: {$exists: false}}) | |
{ "_id" : ObjectId("4e8d0d420ec65305be4f6567"), "username" : "Otgoo", "password" : "foobar" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In SQL: | |
SELECT * FROM users WHERE created_at BETWEEN '2011-05-04' AND '2011-10-10'; | |
In MongoDB: | |
> start = new Date(2011, 5, 4); | |
ISODate("2011-06-03T16:00:00Z") | |
> end = new Date(2011, 10, 10); | |
ISODate("2011-11-09T16:00:00Z") | |
> db.users.find({created_at: {$gte: start, $lt: end}}); | |
{ "_id" : ObjectId("4e8c2a00de570598c220878b"), "username" : "Zoloo", "created_at" : ISODate("2011-10-04T16:00:00Z"), "password" : "secret", "last_visited_at" : ISODate("2011-10-04T16:00:00Z") } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In SQL: | |
CREATE INDEX uindex ON users(username, created_at); | |
In MongoDB: | |
db.users.ensureIndex({created_at: 1, username: 1}) | |
db.users.getIndexes() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> db.system.js.save({_id: "sum", value: function(x,y){return x+y;}}) | |
> db.eval("sum(2,3);") | |
5 | |
> db.numbers.save({x:4,y:2}); | |
> db.numbers.save({x:4,y:3}); | |
> db.numbers.save({x:3,y:3}); | |
> db.numbers.find({$where: "sum(this.x, this.y)==6"}) | |
{ "_id" : ObjectId("4e91283ecf719df59d2da787"), "x" : 4, "y" : 2 } | |
{ "_id" : ObjectId("4e91284acf719df59d2da789"), "x" : 3, "y" : 3 } |