时间:2021-07-01 10:21:17 帮助过:21人阅读
MongoDB 简单使用
联接数据库
Connection 相关参数
数据库操作
In [10]: c.server_info() //查看服务器相关信息
Out[10]:
{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 [16]: db = c['test'] //选择数据库
In [17]: db.collection_names() //列出当前数据库中所有集合名称
Out[17]: [u'system.indexes', u'fs.files', u'fs.chunks', u'test_gao']
In [23]: db.connection //查看联接信息
Out[23]: Connection('192.168.1.3', 27017)
In [24]: db.create_collection('test_abeen') //创建新集合
Out[24]: Collection(Database(Connection('192.168.1.3', 27017), u'test'), u'test_abeen')
In [25]: db.last_status() //查看上次操作状态
Out[25]: {u'err': None, u'n': 0, u'ok': 1.0}
In [26]: db.name //查看当前数据库名称
Out[26]: u'test'
In [27]: db.profiling_info() //查看配置信息
Out[27]: []
In [28]: db.profiling_level()
Out[28]: 0.0
集合操作
In [32]: c = db.test_abeen //选择集合
In [33]: c.name //查看当前集合名称
Out[33]: u'test_abeen'
In [35]: c.full_name //查看当前集合全名
Out[35]: u'test.test_abeen'
In [36]: c.database //查看当前集合数据库相关信息
Out[36]: Database(Connection('192.168.1.3', 27017), u'test')
In [38]: post = {"author":"Mike","text":"this is a test by abeen"}
In [39]: posts = db.posts
In [40]: posts.insert(post) //向数据库集合插入文档,默认创建集合
Out[40]: ObjectId('4c358492421aa91e70000000')
In [41]: db.collection_names() //显示所有集合名称
Out[41]:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen',
u'posts']
In [42]: posts.find_one() //从集合查找信息
Out[42]:
{u'_id': ObjectId('4c358492421aa91e70000000'),
u'author': u'Mike',
u'text': u'this is a test by abeen'}
In [52]: p.update({"author":"Mike"},{"$set":{"author":"abeen","text":"this is a test by abeen shan shan"}})//更新集合文档信息
In [55]: list(p.find())
Out[55]:
[{u'_id': ObjectId('4c358492421aa91e70000000'),
u'author': u'abeen',
u'text': u'this is a test by abeen shan shan'}]
In [96]: list(posts.find())
Out[96]:
[{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 [97]: posts.remove({"a":"abeen"}) //删除符合条件的文档
In [98]: list(posts.find())
Out[98]:
[{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 [102]: db.collection_names()
Out[102]:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen',
u'posts',
u'doc_abeen']
In [104]: db.drop_collection("doc_abeen") //删除集合
In [105]: db.collection_names()
Out[105]:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen',
u'posts']
代码
find格式
代码
In [138]: posts.rename('test_abeen')//重命名当前集合
In [139]: db.collection_names()
Out[139]:
[u'system.indexes',
u'fs.files',
u'fs.chunks',
u'test_gao',
u'system.users',
u'test_abeen']
In [151]: for post in c.find({"a":"aa"}).sort("a"): //查询并排序列
post
Out[152]: {u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'}
Out[152]: {u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a': u'aa', u'b': u'bb'}
// Query #1 y 为null或不存在
> db.foo.find( { "y" : null } )
{ "_id" : ObjectId("4dc1975312c677fc83b5629f"), "x" : 3, "y" : null }
{ "_id" : ObjectId("4dc1975a12c677fc83b562a0"), "x" : 4 }
// Query #2 y为null的值
> db.foo.find( { "y" : { $type : 10 } } )
{ "_id" : ObjectId("4dc1975312c677fc83b5629f"), "x" : 3, "y" : null }
// Query #3 y不存在的结果
> db.foo.find( { "y" : { $exists : false } } )
{ "_id" : ObjectId("4dc1975a12c677fc83b562a0"), "x" : 4 }