Python操作Mongodb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python操作Mongodb相关的知识,希望对你有一定的参考价值。

 

MongoDB

是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。

 

在高负载的情况下,添加更多的节点,可以保证服务器性能。

 

MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。

 

MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。

技术分享

 

1.创建连接

import pymongo

client = pymongo.MongoClient(mongodb://localhost:27017)
#或
#client = pymongo.MongoClient(‘localhost‘,‘27017‘)

2.连接数据库

 

#操作test数据库
db_name = test db = client[db_name]

 

3.选择要操作的集合(表)

 

collection_set01 = db[set01‘]

 

下面就可以使用collection_set01进行增删改查操作了.

 

 

4.增删改查操作

‘‘‘

###################--插入文档--####################
save() vs insert()
mongodb的save和insert函数都可以向collection里插入数据,但两者是有两个区别
1. save函数实际就是根据参数条件,调用了insert或update函数.如果想插入的数据对象存在,insert函数会报错,而save函数是改变原来的对象;如果想插入的对象不存在,那么它们执行相同的插入操作.这里可以用几个字来概括它们两的区别,即所谓"有则改之,无则加之".
2. insert可以一次性插入一个列表,而不用遍历,效率高, save则需要遍历列表,一个个插入.
‘‘‘

record_l = [
{_id:0,name: zzzzz,age: -27,high: 176},
{_id:1,name: zhangweijian,age: 27,high: 171},
{_id:2,name: zhang,age: 26,high: 173},
{_id:3,name: wei,age: 29,high: 180},
{_id:4,name: weijian,age: 30,high: 158},
{_id:5,name: zhangjian,age: 22,high: 179},
{_id:6,name: zwj,age: 19,high: 166},
{_id:100,name: zwj,age: 19,list:[2,3,5]},
{_id:101,name: zwj,age: 19,list:[1,2,3,4,5,6,7]},
]
try:
    for record in record_l:
        collection_set01.save(record)
except pymongo.errors.DuplicateKeyError:
    print record exists
except Exception as e:
    print e

####################--删除数据--####################
remove()
delete_one(self, filter, collation=None)
delete_many(self, filter, collation=None)
 >>> db.test.count({‘x‘: 1})
      3
      >>> result = db.test.delete_one({‘x‘: 1})
      >>> result.deleted_count
      1
      >>> db.test.count({‘x‘: 1})
      2

    :Parameters:
      - `filter`: A query that matches the document to delete.
      - `collation` (optional): An instance of
        :class:`~pymongo.collation.Collation`. This option is only supported
        on MongoDB 3.4 and above.

    :Returns:
      - An instance of :class:`~pymongo.results.DeleteResult`.
‘‘‘

newinsert1 = {_id:7,comment:test delete}
newinsert2 = {_id:8,comment:test delete}
newinsert3 = {_id:9,comment:test delete}
collection_set01.save(newinsert1)
collection_set01.save(newinsert2)
collection_set01.save(newinsert3)

remove_before = collection_set01.find()
print delete before
for obj in remove_before:
    print obj

collection_set01.delete_many({_id:{$gt:6,$lt:100}})   #删除所有满足条件的文档,删除_id大于6,小于100
collection_set01.delete_one({_id:6})   #删除一条满足条件的文档,删除_id=6
#collection_set01.delete_many({}) #删除整个集合
remove_after = collection_set01.find()
print delete after
for obj in remove_after:
    print obj

#输出结果
delete before
{u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
{u‘high‘: 166, u‘age‘: 19, u‘_id‘: 6, u‘name‘: u‘zwj‘}
{u‘comment‘: u‘test delete‘, u‘_id‘: 7}
{u‘comment‘: u‘test delete‘, u‘_id‘: 8}
{u‘comment‘: u‘test delete‘, u‘_id‘: 9}
delete after
{u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}

‘‘‘
###################--更新数据--####################
replace_one(self, filter, replacement, upsert=False, bypass_document_validation=False, collation=None)
update_one(self, filter, update, upsert=False, bypass_document_validation=False, collation=None)
update_many(self, filter, update, upsert=False, bypass_document_validation=False, collation=None)
‘‘‘

 

collection_set01.replace_one({_id: 1},{comment: after replace_one operation just exists comment(key)})  #replace_one用指定的key-value替代原来所有的key-value
collection_set01.update_one({ "high" : { $gt : 170 } } , { $set : { "comment" : "更新于身高高于170的一条记录"}}) #update_one更新已经对应的key-value,其它不变
collection_set01.update_many({high:{$gt:171}},{$set:{comment:use update_many}})  #同上,能够update所有符合匹配条件的文档

‘‘‘
########################--查询--###################
find(self, filter=None, *args, **kwargs)
find_one(self, filter=None, *args, **kwargs)
params:projection/limit/skip
‘‘‘

 find方法源码

find(self, *args, **kwargs) method of pymongo.collection.Collection instance
    Query the database.

    The `filter` argument is a prototype document that all results
    must match. For example:

    >>> db.test.find({"hello": "world"})

    only matches documents that have a key "hello" with value
    "world".  Matches can have other keys *in addition* to
    "hello". The `projection` argument is used to specify a subset
    of fields that should be included in the result documents. By
    limiting results to a certain subset of fields you can cut
    down on network traffic and decoding time.
Raises :class:`TypeError` if any of the arguments are of improper type. Returns an instance of :class:`~pymongo.cursor.Cursor` corresponding to this query. The :meth:`find` method obeys the :attr:`read_preference` of this :class:`Collection`. :Parameters: - `filter` (optional): a SON object specifying elements which must be present for a document to be included in the result set - `projection` (optional): a list of field names that should be returned in the result set or a dict specifying the fields to include or exclude. If `projection` is a list "_id" will always be returned. Use a dict to exclude fields from the result (e.g. projection={‘_id‘: False}). - `skip` (optional): the number of documents to omit (from the start of the result set) when returning the results - `limit` (optional): the maximum number of results to return - `no_cursor_timeout` (optional): if False (the default), any returned cursor is closed by the server after 10 minutes of inactivity. If set to True, the returned cursor will never time out on the server. Care should be taken to ensure that cursors with no_cursor_timeout turned on are properly closed. - `cursor_type` (optional): the type of cursor to return. The valid options are defined by :class:`~pymongo.cursor.CursorType`: - :attr:`~pymongo.cursor.CursorType.NON_TAILABLE` - the result of this find call will return a standard cursor over the result set. - :attr:`~pymongo.cursor.CursorType.TAILABLE` - the result of this find call will be a tailable cursor - tailable cursors are only for use with capped collections. They are not closed when the last data is retrieved but are kept open and the cursor location marks the final document position. If more data is received iteration of the cursor will continue from the last document received. For details, see the `tailable cursor documentation <http://www.mongodb.org/display/DOCS/Tailable+Cursors>`_. - :attr:`~pymongo.cursor.CursorType.TAILABLE_AWAIT` - the result of this find call will be a tailable cursor with the await flag set. The server will wait for a few seconds after returning the full result set so that it can capture and return additional data added during the query. - :attr:`~pymongo.cursor.CursorType.EXHAUST` - the result of this find call will be an exhaust cursor. MongoDB will stream batched results to the client without waiting for the client to request each batch, reducing latency. See notes on compatibility below. - `sort` (optional): a list of (key, direction) pairs specifying the sort order for this query. See :meth:`~pymongo.cursor.Cursor.sort` for details. - `allow_partial_results` (optional): if True, mongos will return partial results if some shards are down instead of returning an error. - `oplog_replay` (optional): If True, set the oplogReplay query flag. - `modifiers` (optional): A dict specifying the MongoDB `query modifiers`_ that should be used for this query. For example:: >>> db.test.find(modifiers={"$maxTimeMS": 500}) - `batch_size` (optional): Limits the number of documents returned in a single batch. - `manipulate` (optional): **DEPRECATED** - If True (the default), apply any outgoing SON manipulators before returning. - `collation` (optional): An instance of :class:`~pymongo.collation.Collation`. This option is only supported on MongoDB 3.4 and above. .. note:: There are a number of caveats to using :attr:`~pymongo.cursor.CursorType.EXHAUST` as cursor_type: - The `limit` option can not be used with an exhaust cursor. - Exhaust cursors are not supported by mongos and can not be used with a sharded cluster. The :meth:`find` method obeys the :attr:`read_preference` of this :class:`Collection`. :Parameters: - `filter` (optional): a SON object specifying elements which must be present for a document to be included in the result set - `projection` (optional): a list of field names that should be returned in the result set or a dict specifying the fields to include or exclude. If `projection` is a list "_id" will always be returned. Use a dict to exclude fields from the result (e.g. projection={‘_id‘: False}). - `skip` (optional): the number of documents to omit (from the start of the result set) when returning the results - `limit` (optional): the maximum number of results to return - `no_cursor_timeout` (optional): if False (the default), any returned cursor is closed by the server after 10 minutes of inactivity. If set to True, the returned cursor will never time out on the server. Care should be taken to ensure that cursors with no_cursor_timeout turned on are properly closed. - `cursor_type` (optional): the type of cursor to return. The valid options are defined by :class:`~pymongo.cursor.CursorType`: - :attr:`~pymongo.cursor.CursorType.NON_TAILABLE` - the result of this find call will return a standard cursor over the result set. - :attr:`~pymongo.cursor.CursorType.TAILABLE` - the result of this find call will be a tailable cursor - tailable cursors are only for use with capped collections. They are not closed when the last data is retrieved but are kept open and the cursor location marks the final document position. If more data is received iteration of the cursor will continue from the last document received. For details, see the `tailable cursor documentation <http://www.mongodb.org/display/DOCS/Tailable+Cursors>`_. - :attr:`~pymongo.cursor.CursorType.TAILABLE_AWAIT` - the result of this find call will be a tailable cursor with the await flag set. The server will wait for a few seconds after returning the full result set so that it can capture and return additional data added during the query. - :attr:`~pymongo.cursor.CursorType.EXHAUST` - the result of this find call will be an exhaust cursor. MongoDB will stream batched results to the client without waiting for the client to request each batch, reducing latency. See notes on compatibility below. - `sort` (optional): a list of (key, direction) pairs specifying the sort order for this query. See :meth:`~pymongo.cursor.Cursor.sort` for details. - `allow_partial_results` (optional): if True, mongos will return partial results if some shards are down instead of returning an error. - `oplog_replay` (optional): If True, set the oplogReplay query flag. - `modifiers` (optional): A dict specifying the MongoDB `query modifiers`_ that should be used for this query. For example:: >>> db.test.find(modifiers={"$maxTimeMS": 500}) - `batch_size` (optional): Limits the number of documents returned in a single batch. - `manipulate` (optional): **DEPRECATED** - If True (the default), apply any outgoing SON manipulators before returning. - `collation` (optional): An instance of :class:`~pymongo.collation.Collation`. This option is only supported on MongoDB 3.4 and above.

#直接上代码

#1.查询身高小于180的文档
print -------------身高小于180:
print type(collection_set01.find({high:{$lt:180}})) #<class ‘pymongo.cursor.Cursor‘>
for r in collection_set01.find({high:{$lt:180}}):
    print r
print type(collection_set01.find_one({high:{$lt:180}})) #<type ‘dict‘>
print use find_one:,collection_set01.find_one({high:{$lt:180}})[high]
print use find_one:,collection_set01.find_one({high:{$lt:180}})

#2.查询特定键(select key1,key2 from table;)
print -------------查询特定键--------
print -------------查询身高大于170,并只列出_id,high和age字段(使用列表形式_id默认打印出来,可以使用{}忽视_id):
for r in collection_set01.find({high:{$gt:170}},projection=[high,age]):print r
print \n
print --------------skip参数用法
for r in collection_set01.find({high:{$gt:170}},[high,age],skip=1):print r #skip=1跳过第一个匹配到的文档
for r in collection_set01.find({high:{$gt:170}},[high,age]).skip(1):print r #skip=1跳过第一个匹配到的文档
print \n
print --------------limit参数用法
for r in collection_set01.find({high:{$gt:170}},[high,age],limit=1):print r #limit=2限制显示2条文档
print \n
print --------------用{}描述特定键
for r in collection_set01.find({high:{$gt:170}},{high:1,age:1,_id:False}):print r

print ---------------------多条件查询
print collection_set01.find_one({high:{$gt:10},age:{$lt:26,$gt:10}})

#3.$in
print ----------------IN
for r in collection_set01.find({"age":{"$in":[23, 26, 32]}}): print r  # select * from users where age in (23, 26, 32)
#for u in db.users.find({"age":{"$nin":(23, 26, 32)}}): print u # select * from users where age not in (23, 26, 32)

#4.count统计数目
print ----------------count
print collection_set01.find({"age":{"$gt":20}}).count() # select count(*) from set01 where age > 10

#5.$or
print ----------------条件或
print 大于等于29或者小于23
for r in collection_set01.find({"$or":[{"age":{"$lte":23}}, {"age":{"$gte":29}}]}): print r

#6.$exists,是否存在字段
print ------------exists
for r in collection_set01.find({age:{$exists:True}}):print age exists,r # select * from 集合名 where exists 键1
for r in collection_set01.find({age:{$exists:False}}):print age not exists,r # select * from 集合名 where not exists 键1

#7.正则表达式查询
print 正则表达式查询
#method 1
for r in collection_set01.find({name:{$regex:r.*wei.*}}):print r   #找出name字段中包含wei的文档
#method 2
import re
Regex = re.compile(r.*zhang.*,re.IGNORECASE)
for r in collection_set01.find({name:Regex}):print r   #找出name字段中包含wei的文档


#8.sort排序

print --------------------使用sort排序(文档中没有排序的字段也会打印出来,表示最小)
#pymongo.ASCENDING      1
#pymongo.DESCENDING     -1
#sort([[],()]),[],()均可
print --------------age 升序
for r in collection_set01.find().sort([["age",pymongo.ASCENDING]]):print r
print --------------age 降序
for r in collection_set01.find().sort([("age",-1)]):print r
print --------------age升序,high升序
for r in collection_set01.find().sort((("age",pymongo.ASCENDING),("high",pymongo.ASCENDING))):print r
print --------------age升序,high降序
for r in collection_set01.find(sort=[("age",pymongo.ASCENDING),("high",pymongo.ASCENDING)]):print r


#9.$all判断数组属性是否包含全部条件,注意与$in区别

print -------------$all
for r in collection_set01.find({list:{$all:[2,3,4]}}):print r
print -------------$in
for r in collection_set01.find({list:{$in:[2,3,4]}}):print r


#10.$size匹配数组属性元素数量
print -------------$size
print -------------size=3
for r in collection_set01.find({list:{$size:3}}):print r
print -------------size=7
for r in collection_set01.find({list:{$size:7}}):print r

#11.$unset和$set相反表示移除文档属性
print -------------------$unset
print ---before
for r in collection_set01.find({name:weijian}):print r
collection_set01.update({name:weijian},{$unset:{age:1}})
print ---after
for r in collection_set01.find({name:weijian}):print r


#输出结果
-------------查询测试-----------
-------------身高小于180:
<class ‘pymongo.cursor.Cursor‘>
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
<type ‘dict‘>
use find_one: 173
use find_one: {u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
-------------查询特定键--------
-------------查询身高大于170,并只列出_id,high和age字段(使用列表形式_id默认打印出来,可以使用{}忽视_id):
{u‘high‘: 173, u‘age‘: 26, u‘_id‘: 2}
{u‘high‘: 180, u‘age‘: 29, u‘_id‘: 3}
{u‘high‘: 179, u‘age‘: 22, u‘_id‘: 5}
{u‘high‘: 176, u‘age‘: -27, u‘_id‘: 0}


--------------skip参数用法
{u‘high‘: 180, u‘age‘: 29, u‘_id‘: 3}
{u‘high‘: 179, u‘age‘: 22, u‘_id‘: 5}
{u‘high‘: 176, u‘age‘: -27, u‘_id‘: 0}
{u‘high‘: 180, u‘age‘: 29, u‘_id‘: 3}
{u‘high‘: 179, u‘age‘: 22, u‘_id‘: 5}
{u‘high‘: 176, u‘age‘: -27, u‘_id‘: 0}


--------------limit参数用法
{u‘high‘: 173, u‘age‘: 26, u‘_id‘: 2}


--------------用{}描述特定键
{u‘high‘: 173, u‘age‘: 26}
{u‘high‘: 180, u‘age‘: 29}
{u‘high‘: 179, u‘age‘: 22}
{u‘high‘: 176, u‘age‘: -27}
---------------------多条件查询
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
----------------IN
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
----------------count
4
----------------条件或
大于等于29或者小于23
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
------------exists用法
age exists {u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
age exists {u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
age exists {u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
age exists {u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
age exists {u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
age exists {u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
age exists {u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
age not exists {u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
正则表达式查询
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
--------------------使用sort排序(文档中没有排序的字段也会打印出来,表示最小)
--------------age 升序
{u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
--------------age 降序
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
--------------age升序,high升序
{u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
--------------age升序,high降序
{u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
-------------$all
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
-------------$in
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
-------------$size用法
-------------size=3
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
-------------size=7
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
-------------------$unset用法
---before
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
---after
{u‘high‘: 158, u‘_id‘: 4, u‘name‘: u‘weijian‘}

 































































































































































































以上是关于Python操作Mongodb的主要内容,如果未能解决你的问题,请参考以下文章

python3操作MongoDB的crud以及聚合案例,代码可直接运行(python经典编程案例)

学习笔记:python3,代码片段(2017)

Python操作MongoDB数据库

100天精通Python(进阶篇)——第40天:pymongo操作MongoDB数据库基础+代码实战

孤荷凌寒自学python第六十二天学习mongoDB的基本操作并进行简单封装1

Python的数据库mongoDB的入门操作