[Database] MongoDB (1) 基本连接、操作、查询
MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可护展的高性能数据存储解决方案。它的特点是高性能、易部署、易使用,存储数据非常方便。MongoDB 简单使用
联接数据库
In : import pymongo
In : from pymongo import Connection
In : connection = Connection('192.168.1.3', 27017) //创建联接 Connection 相关参数
Connection(]]]]]]]])
数据库操作
代码
In : c.database_names() //列出所有数据库名称
Out:
In : c.server_info() //查看服务器相关信息
Out:
{u'bits': 64,
u'gitVersion': u'nogitversion',
u'ok': 1.0,
u'sysInfo': u'Linux yellow 2.6.24-27-server #1 SMP Fri Mar 12 01:23:09 UTC 2010 x86_64 BOOST_LIB_VERSION=1_40',
u'version': u'1.2.2'}
In : db = c['test'] //选择数据库
In : db.collection_names() //列出当前数据库中所有集合名称
Out:
In : db.connection //查看联接信息
Out: Connection('192.168.1.3', 27017)
In : db.create_collection('test_abeen') //创建新集合
Out: Collection(Database(Connection('192.168.1.3', 27017), u'test'), u'test_abeen')
In : db.last_status() //查看上次操作状态
Out: {u'err': None, u'n': 0, u'ok': 1.0}
In : db.name //查看当前数据库名称
Out: u'test'
In : db.profiling_info() //查看配置信息
Out: []
In : db.profiling_level()
Out: 0.0 集合操作
代码
In : db.collection_names() //查看当前数据库所有集合名称
Out:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen']
In : c = db.test_abeen //选择集合
In : c.name //查看当前集合名称
Out: u'test_abeen'
In : c.full_name //查看当前集合全名
Out: u'test.test_abeen'
In : c.database //查看当前集合数据库相关信息
Out: Database(Connection('192.168.1.3', 27017), u'test')
In : post = {"author":"Mike","text":"this is a test by abeen"}
In : posts = db.posts
In : posts.insert(post) //向数据库集合插入文档,默认创建集合
Out: ObjectId('4c358492421aa91e70000000')
In : db.collection_names() //显示所有集合名称
Out:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen',
u'posts']
In : posts.find_one() //从集合查找信息
Out:
{u'_id': ObjectId('4c358492421aa91e70000000'),
u'author': u'Mike',
u'text': u'this is a test by abeen'}
In : p.update({"author":"Mike"},{"$set":{"author":"abeen","text":"this is a test by abeen shan shan"}})//更新集合文档信息
In : list(p.find())
Out:
[{u'_id': ObjectId('4c358492421aa91e70000000'),
u'author': u'abeen',
u'text': u'this is a test by abeen shan shan'}]
In : list(posts.find())
Out:
[{u'_id': ObjectId('4c358492421aa91e70000000'),
u'author': u'Mike',
u'text': u'this is a test by abeen'},
{u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'},
{u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a': u'aa', u'b': u'bb'},
{u'_id': ObjectId('4c358abb421aa91e70000001'),
u'a': u'abeen',
u'b': u'this bb is updated'}]
In : posts.remove({"a":"abeen"}) //删除符合条件的文档
In : list(posts.find())
Out:
[{u'_id': ObjectId('4c358492421aa91e70000000'),
u'author': u'Mike',
u'text': u'this is a test by abeen'},
{u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'},
{u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a': u'aa', u'b': u'bb'}]
In : db.collection_names()
Out:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen',
u'posts',
u'doc_abeen']
In : db.drop_collection("doc_abeen") //删除集合
In : db.collection_names()
Out:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen',
u'posts']
代码
In : result = db.posts.find({"a":"aa"})//查找
In : type(result)
Out:
In : list(result)
Out:
[{u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'},
{u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a': u'aa', u'b': u'bb'}] find格式
find(]]]]]]]]]])
代码
In : db.posts.count()//当前集合文档数
Out: 3
In : type(db.posts)
Out:
In : posts.rename('test_abeen')//重命名当前集合
In : db.collection_names()
Out:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen']
In : for post in c.find({"a":"aa"}).sort("a"): //查询并排序列
post
Out: {u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'}
Out: {u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a': u'aa', u'b': u'bb'}
1 > db.foo.insert( { x : 1, y : 1 } )
2 > db.foo.insert( { x : 2, y : "string" } )
3 > db.foo.insert( { x : 3, y : null } )
4 > db.foo.insert( { x : 4 } )
5
6 // Query #1 y 为null或不存在
7 > db.foo.find( { "y" : null } )
8 { "_id" : ObjectId("4dc1975312c677fc83b5629f"), "x" : 3, "y" : null }
9 { "_id" : ObjectId("4dc1975a12c677fc83b562a0"), "x" : 4 }
10
11 // Query #2 y为null的值
12 > db.foo.find( { "y" : { $type : 10 } } )
13 { "_id" : ObjectId("4dc1975312c677fc83b5629f"), "x" : 3, "y" : null }
14
15 // Query #3 y不存在的结果
16 > db.foo.find( { "y" : { $exists : false } } )
17 { "_id" : ObjectId("4dc1975a12c677fc83b562a0"), "x" : 4 }
-----------------------------------------------------------------------------
更多相关信息
http://api.mongodb.org/python/1.7%2B/api/pymongo/database.html
http://api.mongodb.org/python/1.7%2B/api/pymongo/collection.html
http://api.mongodb.org/python/1.7%2B/api/index.html
页:
[1]