MongoDB: 2. Basic Usage
来源:程序员人生 发布时间:2014-04-02 12:05:07 阅读次数:2659次
须安装 PyMongo (Documentation)。
$ sudo easy_install -U pymongo
1. INSERT使用 insert 插入文档。
> use blogswitched 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 += 326> db.users.save(u2)> db.users.find(){ "_id" : ObjectId("4c44fe0edef8f3492fe67d60"), "name" : "user1", "age" : 26 }
save() 可插入新文档,也可以更新(update)一个已有的文档。
Python:
>>> 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. QueryMongoDB 支持多种复杂的查询方式,能实现大多数 T-SQL 功能,远不是 Key-Value 之类的 NoSQL DB 所能比拟的。
相关函数操作看上去非常像 .NET/C# Linq Method Syntax。
有关查询优化和索引的细节请参考后文。
先准备好所需的测试数据。
> use blogswitched 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,后者返回单个文档。
下面的示例代码中 ">" 是 mongo JS 代码,">>>" 是 Python 代码。
(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({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'}
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠