Skip to content

Instantly share code, notes, and snippets.

View zolzaya's full-sized avatar

Zolzaya Erdenebaatar zolzaya

View GitHub Profile
@zolzaya
zolzaya / json_extended.js
Created September 15, 2011 10:44
JSON extended
{
"forename": "Peter"
, "surname": "Membrey"
, "phone_numbers": [
"+852 1234 5678"
, "+44 1234 565 555"
]
, "emails": [
"[email protected]"
]
@zolzaya
zolzaya / mongodb_basic.sql
Created October 5, 2011 09:59
MongoDB - Basic
> 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)
@zolzaya
zolzaya / first_user.js
Created October 5, 2011 10:22
First user
> 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)
> 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")
}
> db.users.dataSize
function () {
return this.stats().size;
}
> 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})
> 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" }
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") }
In SQL:
CREATE INDEX uindex ON users(username, created_at);
In MongoDB:
db.users.ensureIndex({created_at: 1, username: 1})
db.users.getIndexes()
> 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 }