须安装 PyMongo
(注: 以下 > 提示符表示 mongo JS 代码,>>> 为 Python 代码)
1. INSERT
使用 insert 插入文档。
> use blog switched to db blog > u = { name:"user1", age:23 } { "name" : "user1", "age" : 23 } > db.users.insert(u) > u2 = db.users.findOne({name:"user1"}) { "_id" : ObjectId("4c44fe0edef8f3492fe67d60"), "name" : "user1", "age" : 23 } > u2.age += 3 26 > db.users.save(u2) > db.users.find() { "_id" : ObjectId("4c44fe0edef8f3492fe67d60"), "name" : "user1", "age" : 26 }
>>> import pymongo >>> conn = pymongo.Connection(host="192.168.1.202") >>> db = conn.blog >>> u = {"name":"user1", "age":23} >>> db.users.save(u) ObjectId('4c456e0a499b14047e000000') >>> u2 = db.users.find_one({"name":"user1"}) >>> u2 {u'age': 23, u'_id': ObjectId('4c456e0a499b14047e000000'), u'name': u'user1'} >>> u2["age"] += 3 >>> db.users.save(u2) ObjectId('4c456e0a499b14047e000000') >>> for u in db.users.find(): print u ... {u'age': 26, u'_id': ObjectId('4c456e0a499b14047e000000'), u'name': u'user1'}
> use blog switched to db blog > for (var i = 0; i < 16; i++) db.users.insert({name:"user" + i, age:20 + i, sex:i % 2}) > db.users.find() { "_id" : ObjectId("4c452c343d48c8f284b388df"), "name" : "user0", "age" : 20, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e0"), "name" : "user1", "age" : 21, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e1"), "name" : "user2", "age" : 22, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e2"), "name" : "user3", "age" : 23, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e3"), "name" : "user4", "age" : 24, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e4"), "name" : "user5", "age" : 25, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e5"), "name" : "user6", "age" : 26, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e6"), "name" : "user7", "age" : 27, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e7"), "name" : "user8", "age" : 28, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e8"), "name" : "user9", "age" : 29, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e9"), "name" : "user10", "age" : 30, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388ea"), "name" : "user11", "age" : 31, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388eb"), "name" : "user12", "age" : 32, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388ec"), "name" : "user13", "age" : 33, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388ed"), "name" : "user14", "age" : 34, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388ee"), "name" : "user15", "age" : 35, "sex" : 1 }
> db.users.find({name:"user1"}) { "_id" : ObjectId("4c4528a0b55f2224d447e4b0"), "name" : "user1", "age" : 21, "sex" : 1 } >>> for u in db.users.find({"name":"user1"}): print u ... {u'age': 21.0, u'_id': ObjectId('4c4528a0b55f2224d447e4b0'), u'name': u'user1', u'sex': 1.0}
> db.users.find({name:"user1", age:21}) { "_id" : ObjectId("4c4528a0b55f2224d447e4b0"), "name" : "user1", "age" : 21, "sex" : 1 } >>> for u in db.users.find({"name":"user1", "age":21}): print u ... {u'age': 21.0, u'_id': ObjectId('4c4528a0b55f2224d447e4b0'), u'name': u'user1', u'sex': 1.0}
> db.users.find({age:21}, {'name':1, 'age':1}) { "_id" : ObjectId("4c452c343d48c8f284b388e0"), "name" : "user1", "age" : 21 } >>> for u in db.users.find({"age":21}, ["name", "age"]): print u ... {u'age': 21.0, u'_id': ObjectId('4c452c343d48c8f284b388e0'), u'name': u'user1'}
> db.users.find({}, {'name':1, 'age':1}) { "_id" : ObjectId("4c452c343d48c8f284b388df"), "name" : "user0", "age" : 20 } { "_id" : ObjectId("4c452c343d48c8f284b388e0"), "name" : "user1", "age" : 21 } { "_id" : ObjectId("4c452c343d48c8f284b388e1"), "name" : "user2", "age" : 22 } { "_id" : ObjectId("4c452c343d48c8f284b388e2"), "name" : "user3", "age" : 23 } { "_id" : ObjectId("4c452c343d48c8f284b388e3"), "name" : "user4", "age" : 24 } { "_id" : ObjectId("4c452c343d48c8f284b388e4"), "name" : "user5", "age" : 25 } { "_id" : ObjectId("4c452c343d48c8f284b388e5"), "name" : "user6", "age" : 26 } { "_id" : ObjectId("4c452c343d48c8f284b388e6"), "name" : "user7", "age" : 27 } { "_id" : ObjectId("4c452c343d48c8f284b388e7"), "name" : "user8", "age" : 28 } { "_id" : ObjectId("4c452c343d48c8f284b388e8"), "name" : "user9", "age" : 29 } { "_id" : ObjectId("4c452c343d48c8f284b388e9"), "name" : "user10", "age" : 30 } { "_id" : ObjectId("4c452c343d48c8f284b388ea"), "name" : "user11", "age" : 31 } { "_id" : ObjectId("4c452c343d48c8f284b388eb"), "name" : "user12", "age" : 32 } { "_id" : ObjectId("4c452c343d48c8f284b388ec"), "name" : "user13", "age" : 33 } { "_id" : ObjectId("4c452c343d48c8f284b388ed"), "name" : "user14", "age" : 34 } { "_id" : ObjectId("4c452c343d48c8f284b388ee"), "name" : "user15", "age" : 35 } >>> for u in db.users.find(fields = ["name", "age"]): print u ... {u'age': 20.0, u'_id': ObjectId('4c452c343d48c8f284b388df'), u'name': u'user0'} {u'age': 21.0, u'_id': ObjectId('4c452c343d48c8f284b388e0'), u'name': u'user1'} {u'age': 22.0, u'_id': ObjectId('4c452c343d48c8f284b388e1'), u'name': u'user2'} {u'age': 23.0, u'_id': ObjectId('4c452c343d48c8f284b388e2'), u'name': u'user3'} {u'age': 24.0, u'_id': ObjectId('4c452c343d48c8f284b388e3'), u'name': u'user4'} {u'age': 25.0, u'_id': ObjectId('4c452c343d48c8f284b388e4'), u'name': u'user5'} {u'age': 26.0, u'_id': ObjectId('4c452c343d48c8f284b388e5'), u'name': u'user6'} {u'age': 27.0, u'_id': ObjectId('4c452c343d48c8f284b388e6'), u'name': u'user7'} {u'age': 28.0, u'_id': ObjectId('4c452c343d48c8f284b388e7'), u'name': u'user8'} {u'age': 29.0, u'_id': ObjectId('4c452c343d48c8f284b388e8'), u'name': u'user9'} {u'age': 30.0, u'_id': ObjectId('4c452c343d48c8f284b388e9'), u'name': u'user10'} {u'age': 31.0, u'_id': ObjectId('4c452c343d48c8f284b388ea'), u'name': u'user11'} {u'age': 32.0, u'_id': ObjectId('4c452c343d48c8f284b388eb'), u'name': u'user12'} {u'age': 33.0, u'_id': ObjectId('4c452c343d48c8f284b388ec'), u'name': u'user13'} {u'age': 34.0, u'_id': ObjectId('4c452c343d48c8f284b388ed'), u'name': u'user14'} {u'age': 35.0, u'_id': ObjectId('4c452c343d48c8f284b388ee'), u'name': u'user15'}
> db.users.find().sort({age:1}) { "_id" : ObjectId("4c452c343d48c8f284b388df"), "name" : "user0", "age" : 20, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e0"), "name" : "user1", "age" : 21, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e1"), "name" : "user2", "age" : 22, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e2"), "name" : "user3", "age" : 23, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e3"), "name" : "user4", "age" : 24, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e4"), "name" : "user5", "age" : 25, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e5"), "name" : "user6", "age" : 26, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e6"), "name" : "user7", "age" : 27, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e7"), "name" : "user8", "age" : 28, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e8"), "name" : "user9", "age" : 29, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e9"), "name" : "user10", "age" : 30, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388ea"), "name" : "user11", "age" : 31, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388eb"), "name" : "user12", "age" : 32, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388ec"), "name" : "user13", "age" : 33, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388ed"), "name" : "user14", "age" : 34, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388ee"), "name" : "user15", "age" : 35, "sex" : 1 } > db.users.find().sort({age:-1}) { "_id" : ObjectId("4c452c343d48c8f284b388ee"), "name" : "user15", "age" : 35, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388ed"), "name" : "user14", "age" : 34, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388ec"), "name" : "user13", "age" : 33, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388eb"), "name" : "user12", "age" : 32, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388ea"), "name" : "user11", "age" : 31, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e9"), "name" : "user10", "age" : 30, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e8"), "name" : "user9", "age" : 29, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e7"), "name" : "user8", "age" : 28, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e6"), "name" : "user7", "age" : 27, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e5"), "name" : "user6", "age" : 26, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e4"), "name" : "user5", "age" : 25, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e3"), "name" : "user4", "age" : 24, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e2"), "name" : "user3", "age" : 23, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e1"), "name" : "user2", "age" : 22, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e0"), "name" : "user1", "age" : 21, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388df"), "name" : "user0", "age" : 20, "sex" : 0 } >>> for u in db.users.find().sort([("age", ASCENDING)]): print u ... {u'age': 20.0, u'_id': ObjectId('4c452c343d48c8f284b388df'), u'name': u'user0', u'sex': 0.0} {u'age': 21.0, u'_id': ObjectId('4c452c343d48c8f284b388e0'), u'name': u'user1', u'sex': 1.0} {u'age': 22.0, u'_id': ObjectId('4c452c343d48c8f284b388e1'), u'name': u'user2', u'sex': 0.0} {u'age': 23.0, u'_id': ObjectId('4c452c343d48c8f284b388e2'), u'name': u'user3', u'sex': 1.0} {u'age': 24.0, u'_id': ObjectId('4c452c343d48c8f284b388e3'), u'name': u'user4', u'sex': 0.0} {u'age': 25.0, u'_id': ObjectId('4c452c343d48c8f284b388e4'), u'name': u'user5', u'sex': 1.0} {u'age': 26.0, u'_id': ObjectId('4c452c343d48c8f284b388e5'), u'name': u'user6', u'sex': 0.0} {u'age': 27.0, u'_id': ObjectId('4c452c343d48c8f284b388e6'), u'name': u'user7', u'sex': 1.0} {u'age': 28.0, u'_id': ObjectId('4c452c343d48c8f284b388e7'), u'name': u'user8', u'sex': 0.0} {u'age': 29.0, u'_id': ObjectId('4c452c343d48c8f284b388e8'), u'name': u'user9', u'sex': 1.0} {u'age': 30.0, u'_id': ObjectId('4c452c343d48c8f284b388e9'), u'name': u'user10', u'sex': 0.0} {u'age': 31.0, u'_id': ObjectId('4c452c343d48c8f284b388ea'), u'name': u'user11', u'sex': 1.0} {u'age': 32.0, u'_id': ObjectId('4c452c343d48c8f284b388eb'), u'name': u'user12', u'sex': 0.0} {u'age': 33.0, u'_id': ObjectId('4c452c343d48c8f284b388ec'), u'name': u'user13', u'sex': 1.0} {u'age': 34.0, u'_id': ObjectId('4c452c343d48c8f284b388ed'), u'name': u'user14', u'sex': 0.0} {u'age': 35.0, u'_id': ObjectId('4c452c343d48c8f284b388ee'), u'name': u'user15', u'sex': 1.0} >>> for u in db.users.find().sort([("age", DESCENDING)]): print u ... {u'age': 35.0, u'_id': ObjectId('4c452c343d48c8f284b388ee'), u'name': u'user15', u'sex': 1.0} {u'age': 34.0, u'_id': ObjectId('4c452c343d48c8f284b388ed'), u'name': u'user14', u'sex': 0.0} {u'age': 33.0, u'_id': ObjectId('4c452c343d48c8f284b388ec'), u'name': u'user13', u'sex': 1.0} {u'age': 32.0, u'_id': ObjectId('4c452c343d48c8f284b388eb'), u'name': u'user12', u'sex': 0.0} {u'age': 31.0, u'_id': ObjectId('4c452c343d48c8f284b388ea'), u'name': u'user11', u'sex': 1.0} {u'age': 30.0, u'_id': ObjectId('4c452c343d48c8f284b388e9'), u'name': u'user10', u'sex': 0.0} {u'age': 29.0, u'_id': ObjectId('4c452c343d48c8f284b388e8'), u'name': u'user9', u'sex': 1.0} {u'age': 28.0, u'_id': ObjectId('4c452c343d48c8f284b388e7'), u'name': u'user8', u'sex': 0.0} {u'age': 27.0, u'_id': ObjectId('4c452c343d48c8f284b388e6'), u'name': u'user7', u'sex': 1.0} {u'age': 26.0, u'_id': ObjectId('4c452c343d48c8f284b388e5'), u'name': u'user6', u'sex': 0.0} {u'age': 25.0, u'_id': ObjectId('4c452c343d48c8f284b388e4'), u'name': u'user5', u'sex': 1.0} {u'age': 24.0, u'_id': ObjectId('4c452c343d48c8f284b388e3'), u'name': u'user4', u'sex': 0.0} {u'age': 23.0, u'_id': ObjectId('4c452c343d48c8f284b388e2'), u'name': u'user3', u'sex': 1.0} {u'age': 22.0, u'_id': ObjectId('4c452c343d48c8f284b388e1'), u'name': u'user2', u'sex': 0.0} {u'age': 21.0, u'_id': ObjectId('4c452c343d48c8f284b388e0'), u'name': u'user1', u'sex': 1.0} {u'age': 20.0, u'_id': ObjectId('4c452c343d48c8f284b388df'), u'name': u'user0', u'sex': 0.0}
> db.users.find().sort({sex:1, age:-1}) { "_id" : ObjectId("4c452c343d48c8f284b388ed"), "name" : "user14", "age" : 34, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388eb"), "name" : "user12", "age" : 32, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e9"), "name" : "user10", "age" : 30, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e7"), "name" : "user8", "age" : 28, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e5"), "name" : "user6", "age" : 26, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e3"), "name" : "user4", "age" : 24, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e1"), "name" : "user2", "age" : 22, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388df"), "name" : "user0", "age" : 20, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388ee"), "name" : "user15", "age" : 35, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388ec"), "name" : "user13", "age" : 33, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388ea"), "name" : "user11", "age" : 31, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e8"), "name" : "user9", "age" : 29, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e6"), "name" : "user7", "age" : 27, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e4"), "name" : "user5", "age" : 25, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e2"), "name" : "user3", "age" : 23, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e0"), "name" : "user1", "age" : 21, "sex" : 1 } >>> for u in db.users.find().sort([("sex", ASCENDING), ("age", DESCENDING)]): print u ... {u'age': 34.0, u'_id': ObjectId('4c452c343d48c8f284b388ed'), u'name': u'user14', u'sex': 0.0} {u'age': 32.0, u'_id': ObjectId('4c452c343d48c8f284b388eb'), u'name': u'user12', u'sex': 0.0} {u'age': 30.0, u'_id': ObjectId('4c452c343d48c8f284b388e9'), u'name': u'user10', u'sex': 0.0} {u'age': 28.0, u'_id': ObjectId('4c452c343d48c8f284b388e7'), u'name': u'user8', u'sex': 0.0} {u'age': 26.0, u'_id': ObjectId('4c452c343d48c8f284b388e5'), u'name': u'user6', u'sex': 0.0} {u'age': 24.0, u'_id': ObjectId('4c452c343d48c8f284b388e3'), u'name': u'user4', u'sex': 0.0} {u'age': 22.0, u'_id': ObjectId('4c452c343d48c8f284b388e1'), u'name': u'user2', u'sex': 0.0} {u'age': 20.0, u'_id': ObjectId('4c452c343d48c8f284b388df'), u'name': u'user0', u'sex': 0.0} {u'age': 35.0, u'_id': ObjectId('4c452c343d48c8f284b388ee'), u'name': u'user15', u'sex': 1.0} {u'age': 33.0, u'_id': ObjectId('4c452c343d48c8f284b388ec'), u'name': u'user13', u'sex': 1.0} {u'age': 31.0, u'_id': ObjectId('4c452c343d48c8f284b388ea'), u'name': u'user11', u'sex': 1.0} {u'age': 29.0, u'_id': ObjectId('4c452c343d48c8f284b388e8'), u'name': u'user9', u'sex': 1.0} {u'age': 27.0, u'_id': ObjectId('4c452c343d48c8f284b388e6'), u'name': u'user7', u'sex': 1.0} {u'age': 25.0, u'_id': ObjectId('4c452c343d48c8f284b388e4'), u'name': u'user5', u'sex': 1.0} {u'age': 23.0, u'_id': ObjectId('4c452c343d48c8f284b388e2'), u'name': u'user3', u'sex': 1.0} {u'age': 21.0, u'_id': ObjectId('4c452c343d48c8f284b388e0'), u'name': u'user1', u'sex': 1.0} >>> for u in db.users.find(sort = [("sex", ASCENDING), ("age", DESCENDING)]): print u ... {u'age': 34.0, u'_id': ObjectId('4c452c343d48c8f284b388ed'), u'name': u'user14', u'sex': 0.0} {u'age': 32.0, u'_id': ObjectId('4c452c343d48c8f284b388eb'), u'name': u'user12', u'sex': 0.0} {u'age': 30.0, u'_id': ObjectId('4c452c343d48c8f284b388e9'), u'name': u'user10', u'sex': 0.0} {u'age': 28.0, u'_id': ObjectId('4c452c343d48c8f284b388e7'), u'name': u'user8', u'sex': 0.0} {u'age': 26.0, u'_id': ObjectId('4c452c343d48c8f284b388e5'), u'name': u'user6', u'sex': 0.0} {u'age': 24.0, u'_id': ObjectId('4c452c343d48c8f284b388e3'), u'name': u'user4', u'sex': 0.0} {u'age': 22.0, u'_id': ObjectId('4c452c343d48c8f284b388e1'), u'name': u'user2', u'sex': 0.0} {u'age': 20.0, u'_id': ObjectId('4c452c343d48c8f284b388df'), u'name': u'user0', u'sex': 0.0} {u'age': 35.0, u'_id': ObjectId('4c452c343d48c8f284b388ee'), u'name': u'user15', u'sex': 1.0} {u'age': 33.0, u'_id': ObjectId('4c452c343d48c8f284b388ec'), u'name': u'user13', u'sex': 1.0} {u'age': 31.0, u'_id': ObjectId('4c452c343d48c8f284b388ea'), u'name': u'user11', u'sex': 1.0} {u'age': 29.0, u'_id': ObjectId('4c452c343d48c8f284b388e8'), u'name': u'user9', u'sex': 1.0} {u'age': 27.0, u'_id': ObjectId('4c452c343d48c8f284b388e6'), u'name': u'user7', u'sex': 1.0} {u'age': 25.0, u'_id': ObjectId('4c452c343d48c8f284b388e4'), u'name': u'user5', u'sex': 1.0} {u'age': 23.0, u'_id': ObjectId('4c452c343d48c8f284b388e2'), u'name': u'user3', u'sex': 1.0} {u'age': 21.0, u'_id': ObjectId('4c452c343d48c8f284b388e0'), u'name': u'user1', u'sex': 1.0}
> db.users.find().skip(2).limit(3) { "_id" : ObjectId("4c452c343d48c8f284b388e1"), "name" : "user2", "age" : 22, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e2"), "name" : "user3", "age" : 23, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e3"), "name" : "user4", "age" : 24, "sex" : 0 } >>> for u in db.users.find().skip(2).limit(3): print u ... {u'age': 22.0, u'_id': ObjectId('4c452c343d48c8f284b388e1'), u'name': u'user2', u'sex': 0.0} {u'age': 23.0, u'_id': ObjectId('4c452c343d48c8f284b388e2'), u'name': u'user3', u'sex': 1.0} {u'age': 24.0, u'_id': ObjectId('4c452c343d48c8f284b388e3'), u'name': u'user4', u'sex': 0.0} >>> for u in db.users.find(skip = 2, limit = 3): print u ... {u'age': 22.0, u'_id': ObjectId('4c452c343d48c8f284b388e1'), u'name': u'user2', u'sex': 0.0} {u'age': 23.0, u'_id': ObjectId('4c452c343d48c8f284b388e2'), u'name': u'user3', u'sex': 1.0} {u'age': 24.0, u'_id': ObjectId('4c452c343d48c8f284b388e3'), u'name': u'user4', u'sex': 0.0}
>>> for u in db.users.find()[2:5]: print u ... {u'age': 22.0, u'_id': ObjectId('4c452c343d48c8f284b388e1'), u'name': u'user2', u'sex': 0.0} {u'age': 23.0, u'_id': ObjectId('4c452c343d48c8f284b388e2'), u'name': u'user3', u'sex': 1.0} {u'age': 24.0, u'_id': ObjectId('4c452c343d48c8f284b388e3'), u'name': u'user4', u'sex': 0.0}
> db.users.find({sex:1, age:{$gt:23, $lt:28}}) { "_id" : ObjectId("4c452c343d48c8f284b388e4"), "name" : "user5", "age" : 25, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e6"), "name" : "user7", "age" : 27, "sex" : 1 } >>> for u in db.users.find({"sex":1, "age":{"$gt":23, "$lt":28}}): print u ... {u'age': 25.0, u'_id': ObjectId('4c452c343d48c8f284b388e4'), u'name': u'user5', u'sex': 1.0} {u'age': 27.0, u'_id': ObjectId('4c452c343d48c8f284b388e6'), u'name': u'user7', u'sex': 1.0}
> db.users.find({age:{$in:[23,26,32]}}) { "_id" : ObjectId("4c452c343d48c8f284b388e2"), "name" : "user3", "age" : 23, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e5"), "name" : "user6", "age" : 26, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388eb"), "name" : "user12", "age" : 32, "sex" : 0 } >>> for u in db.users.find({"age":{"$in":(23, 26, 32)}}): print u ... {u'age': 23.0, u'_id': ObjectId('4c452c343d48c8f284b388e2'), u'name': u'user3', u'sex': 1.0} {u'age': 26.0, u'_id': ObjectId('4c452c343d48c8f284b388e5'), u'name': u'user6', u'sex': 0.0} {u'age': 32.0, u'_id': ObjectId('4c452c343d48c8f284b388eb'), u'name': u'user12', u'sex': 0.0}
> db.users.find({age:{$gt:30}}).count() 5 >>> db.users.find({"age":{"$gt":30}}).count() 5
> db.users.find({$or:[{age:25}, {age:28}]}) { "_id" : ObjectId("4c452c343d48c8f284b388e4"), "name" : "user5", "age" : 25, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e7"), "name" : "user8", "age" : 28, "sex" : 0 } > db.users.find({$or:[{age:{$lte:23}}, {age:{$gte:33}}]}) { "_id" : ObjectId("4c452c343d48c8f284b388df"), "name" : "user0", "age" : 20, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e0"), "name" : "user1", "age" : 21, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388e1"), "name" : "user2", "age" : 22, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388e2"), "name" : "user3", "age" : 23, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388ec"), "name" : "user13", "age" : 33, "sex" : 1 } { "_id" : ObjectId("4c452c343d48c8f284b388ed"), "name" : "user14", "age" : 34, "sex" : 0 } { "_id" : ObjectId("4c452c343d48c8f284b388ee"), "name" : "user15", "age" : 35, "sex" : 1 } >>> for u in db.users.find({"$or":[{"age":25}, {"age":28}]}): print u ... {u'age': 25.0, u'_id': ObjectId('4c452c343d48c8f284b388e4'), u'name': u'user5', u'sex': 1.0} {u'age': 28.0, u'_id': ObjectId('4c452c343d48c8f284b388e7'), u'name': u'user8', u'sex': 0.0} >>> for u in db.users.find({"$or":[{"age":{"$lte":23}}, {"age":{"$gte":33}}]}): print u ... {u'age': 20.0, u'_id': ObjectId('4c452c343d48c8f284b388df'), u'name': u'user0', u'sex': 0.0} {u'age': 21.0, u'_id': ObjectId('4c452c343d48c8f284b388e0'), u'name': u'user1', u'sex': 1.0} {u'age': 22.0, u'_id': ObjectId('4c452c343d48c8f284b388e1'), u'name': u'user2', u'sex': 0.0} {u'age': 23.0, u'_id': ObjectId('4c452c343d48c8f284b388e2'), u'name': u'user3', u'sex': 1.0} {u'age': 33.0, u'_id': ObjectId('4c452c343d48c8f284b388ec'), u'name': u'user13', u'sex': 1.0} {u'age': 34.0, u'_id': ObjectId('4c452c343d48c8f284b388ed'), u'name': u'user14', u'sex': 0.0} {u'age': 35.0, u'_id': ObjectId('4c452c343d48c8f284b388ee'), u'name': u'user15', u'sex': 1.0}
> db.users.update({name:"user1"}, {$set:{age:100, sex:0}}) >>> db.users.update({"name":"user1"}, {"$set":{"age":100, "sex":0}})
db.collection.update(criteria, objNew, upsert, mult)
> db.users.update({}, {$inc:{age:10}}, false, true) >>> db.users.update({}, {"$inc":{"age":10}}, multi=True)
> db.users.update({name:"user1"}, {$inc:{age:10}, $set:{sex:1}}) >>> db.users.update({"name":"user1"}, {"$inc":{"age":10}, "$set":{"sex":1}})
> use blog switched to db blog > for (var i = 0; i < 10; i++) db.users.insert({name : "user" + i, age : 20 + i}) > db.users.find() { "_id" : ObjectId("4c4508818c4a1e0bf570460d"), "name" : "user0", "age" : 20 } { "_id" : ObjectId("4c4508818c4a1e0bf570460e"), "name" : "user1", "age" : 21 } { "_id" : ObjectId("4c4508818c4a1e0bf570460f"), "name" : "user2", "age" : 22 } { "_id" : ObjectId("4c4508818c4a1e0bf5704610"), "name" : "user3", "age" : 23 } { "_id" : ObjectId("4c4508818c4a1e0bf5704611"), "name" : "user4", "age" : 24 } { "_id" : ObjectId("4c4508818c4a1e0bf5704612"), "name" : "user5", "age" : 25 } { "_id" : ObjectId("4c4508818c4a1e0bf5704613"), "name" : "user6", "age" : 26 } { "_id" : ObjectId("4c4508818c4a1e0bf5704614"), "name" : "user7", "age" : 27 } { "_id" : ObjectId("4c4508818c4a1e0bf5704615"), "name" : "user8", "age" : 28 } { "_id" : ObjectId("4c4508818c4a1e0bf5704616"), "name" : "user9", "age" : 29 } > id = db.users.findOne({name:"user2"})._id ObjectId("4c4508818c4a1e0bf570460f") > db.users.remove(id) > db.users.find() { "_id" : ObjectId("4c4508818c4a1e0bf570460d"), "name" : "user0", "age" : 20 } { "_id" : ObjectId("4c4508818c4a1e0bf570460e"), "name" : "user1", "age" : 21 } { "_id" : ObjectId("4c4508818c4a1e0bf5704610"), "name" : "user3", "age" : 23 } { "_id" : ObjectId("4c4508818c4a1e0bf5704611"), "name" : "user4", "age" : 24 } { "_id" : ObjectId("4c4508818c4a1e0bf5704612"), "name" : "user5", "age" : 25 } { "_id" : ObjectId("4c4508818c4a1e0bf5704613"), "name" : "user6", "age" : 26 } { "_id" : ObjectId("4c4508818c4a1e0bf5704614"), "name" : "user7", "age" : 27 } { "_id" : ObjectId("4c4508818c4a1e0bf5704615"), "name" : "user8", "age" : 28 } { "_id" : ObjectId("4c4508818c4a1e0bf5704616"), "name" : "user9", "age" : 29 } > db.users.remove() > db.users.find()
>>> for i in range(10): db.users.insert({"name":"user"+str(i), "age":20+i}) ... ObjectId('4c456e8b499b14047e000001') ObjectId('4c456e8b499b14047e000002') ObjectId('4c456e8b499b14047e000003') ObjectId('4c456e8b499b14047e000004') ObjectId('4c456e8b499b14047e000005') ObjectId('4c456e8b499b14047e000006') ObjectId('4c456e8b499b14047e000007') ObjectId('4c456e8b499b14047e000008') ObjectId('4c456e8b499b14047e000009') ObjectId('4c456e8b499b14047e00000a') >>> id = db.users.find_one({"name":"user2"})["_id"] >>> id ObjectId('4c456e8b499b14047e000003') >>> db.users.remove(id) >>> for u in db.users.find(): print u ... {u'age': 20, u'_id': ObjectId('4c456e8b499b14047e000001'), u'name': u'user0'} {u'age': 21, u'_id': ObjectId('4c456e8b499b14047e000002'), u'name': u'user1'} {u'age': 23, u'_id': ObjectId('4c456e8b499b14047e000004'), u'name': u'user3'} {u'age': 24, u'_id': ObjectId('4c456e8b499b14047e000005'), u'name': u'user4'} {u'age': 25, u'_id': ObjectId('4c456e8b499b14047e000006'), u'name': u'user5'} {u'age': 26, u'_id': ObjectId('4c456e8b499b14047e000007'), u'name': u'user6'} {u'age': 27, u'_id': ObjectId('4c456e8b499b14047e000008'), u'name': u'user7'} {u'age': 28, u'_id': ObjectId('4c456e8b499b14047e000009'), u'name': u'user8'} {u'age': 29, u'_id': ObjectId('4c456e8b499b14047e00000a'), u'name': u'user9'} >>> db.users.remove() >>> for u in db.users.find(): print u ... >>>