- 浏览: 3940 次
- 性别:
- 来自: 北京
最新评论
MongoDB:2. Basic Usage
数据库basicbasicusagemongodbdocumentatio
摘要:须安装PyMongo( Documentation )。 $ sudo easy_install -U pymongo (注: 以下 提示符表示 mongo JS 代码, 为 Python 代码) 1. INSERT 使用 insert 插入文档。 useblog switchedtodbblog u={name: user1 ,age:23} { name : user1 , age :23}
须安装PyMongo(Documentation)。
$ sudo easy_install -U 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 }
save() 可插入新文档,也可以更新(update)一个已有的文档。下面是用 PyMongo 写的相同的代码。
>>> 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 }
主要用到的查询函数式 find() 和 findOne(),前者返回一个迭代器 cursor,后者返回单个文档。
(1) WHERE
# select * from users where name = 'user1'
> 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({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'}
(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
...
>>>
数据库basicbasicusagemongodbdocumentatio
摘要:须安装PyMongo( Documentation )。 $ sudo easy_install -U pymongo (注: 以下 提示符表示 mongo JS 代码, 为 Python 代码) 1. INSERT 使用 insert 插入文档。 useblog switchedtodbblog u={name: user1 ,age:23} { name : user1 , age :23}
须安装PyMongo(Documentation)。
$ sudo easy_install -U 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 }
save() 可插入新文档,也可以更新(update)一个已有的文档。下面是用 PyMongo 写的相同的代码。
>>> 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 }
主要用到的查询函数式 find() 和 findOne(),前者返回一个迭代器 cursor,后者返回单个文档。
(1) WHERE
# select * from users where name = 'user1'
> 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({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'}
(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
...
>>>
发表评论
-
MongoDB:5. Admin
2014-06-18 11:16 516MongoDB:5. Admin 浏览器interfac ... -
MongoDB:4. Index
2014-06-18 11:14 502MongoDB:4. Index优化python文档inde ... -
MongoDB:3. Schema Design
2014-06-18 11:11 363MongoDB:3. Schema Design 数据库正则 ... -
MongoDB:1. Database
2014-06-18 11:07 336MongoDB:1. Database authentica ...
相关推荐
Spring集成MongoDB官方指定jar包:spring-data-mongodb-1.4.1.RELEASE.jar
df = spark.read.format(com.mongodb.spark.sql.DefaultSource).load() File /home/cisco/spark-2.2.0-bin-hadoop2.7/python/lib/pyspark.zip/pyspark/sql/readwriter.py, line 165, in load
`mongodb.dll`是MongoDB数据库系统在Windows平台上运行所必需的一个动态链接库(DLL)文件,它包含了MongoDB客户端和服务器所需的关键功能。 在Windows操作系统中,`.dll`文件是一种共享库,用于存储程序执行时需要...
2. **索引机制**:MongoDB提供了类似于关系型数据库的索引机制,用户可以创建多种类型的索引以优化查询性能。与MySQL等关系型数据库类似,MongoDB也支持创建唯一索引、复合索引等多种索引类型。 3. **内存管理**:...
2. MongoDB.Driver.Core.dll:这是MongoDB驱动的核心组件,负责处理与MongoDB服务器的低级别通信。它包含网络连接、命令执行、会话管理等功能,为上层的MongoDB.Driver.dll提供基础支持。MongoDB.Driver.Core.dll...
在Linux系统中,MongoDB是一个常用的文档型数据库管理系统,它为开发者提供了高性能、高可用性和易扩展性的数据存储解决方案。然而,在使用过程中,有时会遇到启动脚本错误,如"env: /etc/init.d/mongodb : no such ...
访问<https://www.mongodb.com/try/download/community>,根据你的系统需求选择合适的版本。下载完成后,运行安装程序,开始安装过程。 在安装过程中,你将遇到几个关键步骤: 1. 接受许可协议:在安装向导中,...
2. 高可扩展性:MongoDB 支持水平扩展,可以根据需要增加节点来提高系统的性能。 3.灵活的数据模型:MongoDB 使用 BSON 存储数据,可以存储复杂的数据结构,如数组和嵌套对象。 4.丰富的查询语法:MongoDB 提供了...
2. **安装与配置**:书中详细讲解了在各种操作系统上安装MongoDB的过程,包括Windows、Linux和macOS。同时,还涵盖了配置文件的设置,如数据存储路径、端口设定、日志记录等。 3. **数据模型**:MongoDB的数据模型...
04mongodb基本概念:文档.mp4
- `nosql.manager.for.mongodb.v.3.1.0.5-patch.exe`:可能是针对该版本 NoSQL Manager 的补丁程序,用于修复问题或增加新功能。 - `URET NFO v2.2.exe` 和 `URET.nfo`:这些文件通常是 P2P 文件分享中包含的信息...
1. **下载安装包**: 首先,你需要从MongoDB官方网站(https://www.mongodb.com/download-center/community)下载适合你操作系统的安装包。对于Windows用户,可以选择.msi安装文件;对于Linux用户,可以选择.tgz或....
2. 安装步骤:用户可以按照安装向导安装MongoDB Community Server。 MongoDB是基于分布式文件存储的NoSQL数据库,具有高性能、灵活的数据模型和可扩展的架构。用户可以使用MongoDB Atlas云数据库或MongoDB ...
3. 在php.ini文件中启用扩展,如`extension=mongodb.so`。 4. 重启PHP服务以使更改生效。 5. 在PHP代码中引入所需的类和方法,开始进行MongoDB的数据库操作。 了解并熟练掌握MongoDB的PHP扩展是开发基于MongoDB的...
- **标题**: ".MongoDB.The.Definitive.Guide" - **描述**: ".MongoDB.The.Definitive.Guide" 这两个字段虽然内容完全相同,但是指代的是同一本书——《MongoDB: The Definitive Guide》。这是一本详细介绍MongoDB...
2. 高可用:MongoDB支持高可用性,可以自动地将数据复制到多个节点上,从而确保数据的安全。 3. 可扩展:MongoDB支持水平扩展,可以根据需要增加节点来提高性能。 4. 自动收缩:MongoDB可以自动地将数据收缩到合适的...
connect-mongo, 用于表示和连接的MongoDB会话存储 连接 mongo用于连接和 Express的MongoDB会话存储 兼容性支持 5.0支持所有连接版本支持 Mongoose >= 4.1.2 支持原生MongoDB驱动程序 >= 2.
MongoDB: The Definitive Guide MongoDB is a powerful, flexible, and scalable generalpurpose database. It combines the ability to scale out with features such as secondary indexes, range queries, ...
2. 数据库管理:用户可以创建、删除和修改数据库,查看数据库大小、存储引擎等信息。同时,它支持数据导入和导出,便于数据迁移和备份。 3. 集合操作:Navicat允许用户创建、删除和重命名集合,查看集合统计信息,...