须安装 PyMongo (Documentation)。
$ sudo easy_install -U pymongo
> 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'}
2. Query
MongoDB 支持多种复杂的查询方式,能实现大多数 T-SQL 功能,远不是 Key-Value 之类的 NoSQL DB 所能比拟的。
相关函数操作看上去非常像 .NET/C# Linq Method Syntax。
有关查询优化和索引的细节请参考后文。
先准备好所需的测试数据。
> 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}
# select * from users where name = 'user1' and age = 21
> 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}
(2) FIELDS
# select name, age from users where age = 21
> 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'}
# select name, age from users
> 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'}
(3) SORT
# select * from users order by age
> 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}
# select * from users order by sex asce, age desc
> 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}
(4) SLICE
# select * from users skip 2 limit 3
> 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}
可以用切片代替 skip & limit (mongo 中的 $slice 貌似有点问题)。
>>> 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}
(5) Conditional Operators
# select * from users where sex = 1 and age > 23 and age <28
> 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}
比较操作包括:$gt (>)、$lt (<)、$gte (>=)、$lte(<=)、$ne (!=)。
(6) IN
# select * from users where age in (23, 26, 32)
> 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}
对应的操作符有 $nin (not in)。
(7) COUNT
# select count(*) from users where age > 30
> db.users.find({age:{$gt:30}}).count()
5
>>> db.users.find({"age":{"$gt":30}}).count()
5
(8) OR
# select * from users where age = 25 or age = 28
# select * from users where age <= 23 or age >= 33
> 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}
还有一些复杂或专用的查询另文记录。
3. Update
可直接用类似 T-SQL 条件表达式更新,或用 Save() 更新从数据库返回到文档对象。
# update users set age = 100, sex = 0 where name = 'user1'
> db.users.update({name:"user1"}, {$set:{age:100, sex:0}})
>>> db.users.update({"name":"user1"}, {"$set":{"age":100, "sex":0}})
update() 有几个参数需要注意。
db.collection.update(criteria, objNew, upsert, mult)
criteria: 需要被更新的条件表达式
objNew: 更新表达式
upsert: 如目标记录不存在,是否插入新文档。
multi: 是否更新多个文档。
# update users set age = age + 10
> db.users.update({}, {$inc:{age:10}}, false, true)
>>> db.users.update({}, {"$inc":{"age":10}}, multi=True)
# update users set age = age + 10, sex = 1 where name = 'user1'
> db.users.update({name:"user1"}, {$inc:{age:10}, $set:{sex:1}})
>>> db.users.update({"name":"user1"}, {"$inc":{"age":10}, "$set":{"sex":1}})
还有一些针对集合的更新操作另文记录。
4. Remove
remove() 用于删除单个或全部文档,删除后的文档无法恢复。
> 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()
Python:
>>> 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
...
>>>