Switch branches/tags
Nothing to show
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
README.org
cheatsheet-mongodb-A4.pdf

README.org

1 CheatSheet: Mongodb

linkedin
github
slack


PRs Welcome

File me Issues or star this repo.

1.1 Mongodb Operation

NameSummary
Connect to mongodbmongo 192.168.75.108:12345/d_2003 -u u_2003 -p XXXX
Start mongomongod --config /etc/mongod.conf
Stop mongo=kill -15 <pid>; mongo –eval “db.getSiblingDB(‘admin’).shutdownServer()”=
Mongo script get parametersmongo –eval “var day=’2013-07-26’” localhost:27017/shopex mongojs/update-index.js
Mongo data folder/var/lib/mongodb/journal
Export to json filemongoexport --db shopex --collection order --out collection.json
Import json filemongoimport --upsert --db shopex --collection order --file collection.json

1.2 Mongodb Basic

NameSummary
Remove tabledb.kvstore.remove()
define stored proceduredb.system.js.save({_id:"addNumbers", value:function(x, y){ return x + y; }})
view stored proceduredb.system.js.find()
call stored proceduredb.eval('addNumbers(3, 4.2)')
Create recorddb.users.insert({"name":{"first":"refactor","last":"refactor2"},"age":24})
Update recorddb.users.update({"age":30}, {$set:{"age":31}});
Delete by filteringdb.users.remove({"age":30})
Loop cusrorwhile(s.hasNext()) printjson(s.next());
Loop arrayi=0; while(i < s.length) printjson(s[i++]);

1.3 Mongodb Query

NameSummary
Basic querydb.users.find({“name”:{“first”:”refactor”,”last”:”refactor2”}})
Basic querydb.users.find({“name.first”:”refactor”})
great than / or equaldb.users.find({“age”:{“$gte”:20}})
not equaldb.users.find({“age”:{“$ne”:31}})
existsdb.users.find({“age”:{“$exists”:true}})
indb.users.find({“age”:{“$in”:[21,24]}})
not indb.users.find({“age”:{“$nin”:[21,23]}})
ordb.users.find({“$or”:[{“name.last”:”another”}, {“age”:31}]})
Count matched recordsdb.order.find({“created”:{“$regex”:”2013-07-12.*”}}).count()
db.users.find({“age”:{“$nin”:[21,23]}}).count()
query by offsetdb.users.find({“age”:{“$nin”:[21,23]}}).skip(1).limit(2)
sortdb.users.find().sort({“age”:24});
reverse orderdb.users.find().sort({“name.last”: -1})
db.users.find().forEach( function(u) { printjson(u); } );
mod 20db.users.find({“age”:{“$mod”:[20,4]}})
Parse string to floatparseFloat(v.availableConfirmFee, 10);
filter by “like”db.taobao.find({“created”:/2013-07-12.*/})
db.order.find({“created”:/2013-07-14.*/, “num”:{“$exists”:false}}).forEach( function(u) { printjson(u.tid); } );
db.order.find({“created”:{“$regex”:day}}).count();
db.order.distinct(“buyerNick”, {“created”:{“$regex”:day}}).length

1.4 More Resources

License: Code is licensed under MIT License.