Skip to content

Instantly share code, notes, and snippets.

View zolzaya's full-sized avatar

Zolzaya Erdenebaatar zolzaya

View GitHub Profile
> db.users.help()
DBCollection help
db.users.find().help() - show DBCursor help
db.users.count()
db.users.dataSize()
db.users.distinct( key ) - eg. db.users.distinct( 'x' )
db.users.drop() drop the collection
db.users.dropIndex(name)
db.users.dropIndexes()
db.users.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
> db.help()
DB methods:
db.addUser(username, password[, readOnly=false])
db.auth(username, password)
db.cloneDatabase(fromhost)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost)
db.createCollection(name, { size : ..., capped : ..., max : ... } )
db.currentOp() displays the current operation in the db
db.dropDatabase()
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo start mongodb
> 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 }
In SQL:
CREATE INDEX uindex ON users(username, created_at);
In MongoDB:
db.users.ensureIndex({created_at: 1, username: 1})
db.users.getIndexes()
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") }
> 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" }
> 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.dataSize
function () {
return this.stats().size;
}
> 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")
}