python连接mongodb进行查询_Python中的MongoDB基本操作:连接、查询实例

本文展示了如何使用Python连接MongoDB数据库,并进行集合选择、插入、查询、更新和删除操作。通过实例演示了MongoDB的基本操作,包括查看数据库集合、选择集合、插入文档、查找信息以及更新和删除文档。
摘要由CSDN通过智能技术生成

In [31]: db.collection_names() //查看当前数据库所有集合名称

Out[31]:

[u'system.indexes',

u'fs.files',

u'fs.chunks',

u'test_gao',

u'system.users',

u'test_abeen']

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']

代码

复制代码 代码如下:

In [113]: result = db.posts.find({"a":"aa"})//查找

In [114]: type(result)

Out[114]: In [119]: list(result)

Out[119]:

[{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([spec=None[, fields=None[, skip=0[, limit=0[, timeout=True[, snapshot=False[, tailable=False[, sort=None[, max_scan=None[, as_class=None[, **kwargs]]]]]]]]]]])

代码

复制代码 代码如下:

In [120]: db.posts.count()//当前集合文档数

Out[120]: 3

In [121]: type(db.posts)

Out[121]: 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'}

复制代码 代码如下:

> db.foo.insert( { x : 1, y : 1 } )

> db.foo.insert( { x : 2, y : "string" } )

> db.foo.insert( { x : 3, y : null } )

> db.foo.insert( { x : 4 } )

// 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 }

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

相关文章

相关视频

网友评论

文明上网理性发言,请遵守 新闻评论服务协议我要评论

立即提交

专题推荐独孤九贱-php全栈开发教程

全栈 100W+

主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门

玉女心经-web前端开发教程

入门 50W+

主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门

天龙八部-实战开发教程

实战 80W+

主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习

php中文网:公益在线php培训,帮助PHP学习者快速成长!

Copyright 2014-2020 https://www.php.cn/ All Rights Reserved | 苏ICP备2020058653号-1

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值