pymongo的一些操作
参考:http://www.yiibai.com/mongodb/mongodb_drop_collection.html
http://www.cnblogs.com/zhouxuchen/p/5544227.html
pymongo的一些操作:
启动
sudo service mongod start
远程连接的时候, 配置mongodb.conf (通过 $ whereis mongod 找到位置) #bind_ip = 127.0.0.1 改为0.0.0.0 ,和django是一样的
数据库 聚集(1层)
#!/usr/bin/python #coding=utf-8 import datetime,time from pymongo import MongoClient #连接到数据库 # client = MongoClient('localhost', 27017) client = MongoClient("127.0.0.1", 27017) #list all databases print client.database_names() #database list #delete specific database # client.drop_database('tieba') #delete # db = client['tieba'] #没有就新建 #list all collection names print db.collection_names(include_system_collections=False) #delete specific collection #db["mycollection"].drop()
聚集下的item:增删改查(2层)
insert_one,增加一个item,如果你传递给insert_one()
方法的参数不包含_id
字段,MongoClient将自动添加这个字段并且生成一个ObjectId设置为这个字段的值。
def insert_one(self, document, bypass_document_validation=False): """Insert a single document. >>> db.test.count({'x': 1}) 0 >>> result = db.test.insert_one({'x': 1}) >>> result.inserted_id ObjectId('54f112defba522406c9cc208') >>> db.test.find_one({'x': 1}) {u'x': 1, u'_id': ObjectId('54f112defba522406c9cc208')} :Parameters: - `document`: The document to insert. Must be a mutable mapping type. If the document does not have an _id field one will be added automatically. - `bypass_document_validation`: (optional) If ``True``, allows the write to opt-out of document level validation. Default is ``False``.
insert_many,增加一个可循环的item
def insert_many(self, documents, ordered=True, bypass_document_validation=False): """Insert an iterable of documents. >>> db.test.count() 0 >>> result = db.test.insert_many([{'x': i} for i in range(2)]) >>> result.inserted_ids [ObjectId('54f113fffba522406c9cc20e'), ObjectId('54f113fffba522406c9cc20f')] >>> db.test.count() 2 :Parameters: - `documents`: A iterable of documents to insert. - `ordered` (optional): If ``True`` (the default) documents will be inserted on the server serially, in the order provided. If an error occurs all remaining inserts are aborted. If ``False``, documents will be inserted on the server in arbitrary order, possibly in parallel, and all document inserts will be attempted. - `bypass_document_validation`: (optional) If ``True``, allows the write to opt-out of document level validation. Default is ``False``.
update_one,注意一些操作符,如$set,$unset,$inc ,设置,删除,加法,对字段的操作,upsert=True,没有就增加
更新顶级字段
如下操作将更新第一个符合name
等于Juni
这个条件的文档。使用$set
操作符更新cuisine
字段且将lastModified
修改为当前日期。
result = db.restaurants.update_one( {"name": "Juni"}, { "$set": { "cuisine": "American (New)" }, "$currentDate": {"lastModified": True} } )
更新嵌入式文档中的字段
要更新一个嵌入式文档中的字段,需要使用点操作符。当使用点操作符时,使用点操作符需要使用双引号将字段名包裹。下面的操作将更新address
字段中的street
值。
result = db.restaurants.update_one( {"restaurant_id": "41156888"}, {"$set": {"address.street": "East 31st Street"}} )
def update_one(self, filter, update, upsert=False, bypass_document_validation=False, collation=None): """Update a single document matching the filter. >>> for doc in db.test.find(): ... print(doc) ... {u'x': 1, u'_id': 0} {u'x': 1, u'_id': 1} {u'x': 1, u'_id': 2} >>> result = db.test.update_one({'x': 1}, {'$inc': {'x': 3}}) >>> result.matched_count 1 >>> result.modified_count 1 >>> for doc in db.test.find(): ... print(doc) ... {u'x': 4, u'_id': 0} {u'x': 1, u'_id': 1} {u'x': 1, u'_id': 2} :Parameters: - `filter`: A query that matches the document to update. - `update`: The modifications to apply. - `upsert` (optional): If ``True``, perform an insert if no documents match the filter. - `bypass_document_validation`: (optional) If ``True``, allows the write to opt-out of document level validation. Default is ``False``. - `collation` (optional): An instance of :class:`~pymongo.collation.Collation`. This option is only supported on MongoDB 3.4 and above.
update_many,更改了所有匹配的item
def update_many(self, filter, update, upsert=False, bypass_document_validation=False, collation=None): """Update one or more documents that match the filter. >>> for doc in db.test.find(): ... print(doc) ... {u'x': 1, u'_id': 0} {u'x': 1, u'_id': 1} {u'x': 1, u'_id': 2} >>> result = db.test.update_many({'x': 1}, {'$inc': {'x': 3}}) >>> result.matched_count 3 >>> result.modified_count 3 >>> for doc in db.test.find(): ... print(doc) ... {u'x': 4, u'_id': 0} {u'x': 4, u'_id': 1} {u'x': 4, u'_id': 2} :Parameters: - `filter`: A query that matches the documents to update. - `update`: The modifications to apply. - `upsert` (optional): If ``True``, perform an insert if no documents match the filter. - `bypass_document_validation` (optional): If ``True``, allows the write to opt-out of document level validation. Default is ``False``. - `collation` (optional): An instance of :class:`~pymongo.collation.Collation`. This option is only supported on MongoDB 3.4 and above.
replace_one,替换,这个是没有操作符的
替换一个文档
要替换整个文档(除了_id
字段),将一个完整的文档作为第二个参数传给update()
方法。替代文档对应原来的文档可以有不同的字段。在替代文档中,你可以忽略_id
字段因为它是不变的。如果你包含了_id
字段,那它必须和原文档的值相同。
重要:
在更新之后,该文档将只包含替代文档的字段。
在如下的更新操作后,被修改的文档将只剩下_id
、name
和address
字段。该文档将不再包含restaurant_id
、cuisine
、grades
以及borough
字段。
result = db.restaurants.replace_one( {"restaurant_id": "41704620"}, { "name": "Vella 2", "address": { "coord": [-73.9557413, 40.7720266], "building": "1480", "street": "2 Avenue", "zipcode": "10075" } } )
def replace_one(self, filter, replacement, upsert=False, bypass_document_validation=False, collation=None): """Replace a single document matching the filter. >>> for doc in db.test.find({}): ... print(doc) ... {u'x': 1, u'_id': ObjectId('54f4c5befba5220aa4d6dee7')} >>> result = db.test.replace_one({'x': 1}, {'y': 1}) >>> result.matched_count 1 >>> result.modified_count 1 >>> for doc in db.test.find({}): ... print(doc) ... {u'y': 1, u'_id': ObjectId('54f4c5befba5220aa4d6dee7')} The *upsert* option can be used to insert a new document if a matching document does not exist. >>> result = db.test.replace_one({'x': 1}, {'x': 1}, True) >>> result.matched_count 0 >>> result.modified_count 0 >>> result.upserted_id ObjectId('54f11e5c8891e756a6e1abd4') >>> db.test.find_one({'x': 1}) {u'x': 1, u'_id': ObjectId('54f11e5c8891e756a6e1abd4')} :Parameters: - `filter`: A query that matches the document to replace. - `replacement`: The new document. - `upsert` (optional): If ``True``, perform an insert if no documents match the filter. - `bypass_document_validation`: (optional) If ``True``, allows the write to opt-out of document level validation. Default is ``False``. - `collation` (optional): An instance of :class:`~pymongo.collation.Collation`. This option is only supported on MongoDB 3.4 and above.
delete_one,删除一个item
"""Delete a single document matching the filter. >>> 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.
delete_many,只要符合匹配规则的,都删除
要删除一个集合中的所有文档,给delete_many()
方法传递一个空的条件参数即可。
result = db.restaurants.delete_many({})
销毁一个集合
删除所有文档的操作只会清空集合中的文档。该集合以及集合的索引将依旧存在。要清空一个集合,销毁该集合以及它的索引并且重建集合和索引可能是相比于清空一个集合更加高效的操作方式。使用drop()
方法可以销毁一个集合,包括它所有的索引。
db.restaurants.drop()
def delete_many(self, filter, collation=None): """Delete one or more documents matching the filter. >>> db.test.count({'x': 1}) 3 >>> result = db.test.delete_many({'x': 1}) >>> result.deleted_count 3 >>> db.test.count({'x': 1}) 0 :Parameters: - `filter`: A query that matches the documents to delete. - `collation` (optional): An instance of :class:`~pymongo.collation.Collation`. This option is only supported on MongoDB 3.4 and above.
find_one
def find_one(self, filter=None, *args, **kwargs): """Get a single document from the database. All arguments to :meth:`find` are also valid arguments for :meth:`find_one`, although any `limit` argument will be ignored. Returns a single document, or ``None`` if no matching document is found. The :meth:`find_one` method obeys the :attr:`read_preference` of this :class:`Collection`. :Parameters: - `filter` (optional): a dictionary specifying the query to be performed OR any other type to be used as the value for a query for ``"_id"``. - `*args` (optional): any additional positional arguments are the same as the arguments to :meth:`find`. - `**kwargs` (optional): any additional keyword arguments are the same as the arguments to :meth:`find`. >>> collection.find_one(max_time_ms=100) """
find,调用find()
方式不传值即可得到集合中所有的文档,
cursor = db.restaurants.find()
返回restaurants
集合中所有文档。
如下所示的操作将查询borough
字段等于Manhattan
的文档。
cursor = db.restaurants.find({"borough": "Manhattan"})
逻辑与
你可以使用一个列表指定一个逻辑与条件查询操作,使用逗号分隔条件。
cursor = db.restaurants.find({"cuisine": "Italian", "address.zipcode": "10075"})
逻辑或
你可以使用$or
操作符进行逻辑或条件的指定。
cursor = db.restaurants.find( {"$or": [{"cuisine": "Italian"}, {"address.zipcode": "10075"}]})
大于($gt)操作符,操作符:$eq $gt $gte $in $lt $lte $ne $nin,等等,还有很多
查询字段grades
包含一个嵌入式文档,其中score
大于30。
cursor = db.restaurants.find({"grades.score": {"$gt": 30}})
对结果进行排序
要指定结果集的顺序,可以通过追加sort()
方法进行查询。给sort()
方法传递需要排序的字段和配需类型等。
pymongo.ASCENDING
表示升序排序。pymongo.DESCENDING
表示降序排序。
如果要通过多个键星星排序,可以传递键的列表和以及对应的排序类型的列表。举例来说,如下操作将返回restaurants
集合中所有的文档,并且先通过borough
字段进行升序排序,然后在每个borough
内,通过"address.zipcode"
字段进行升序排序。
import pymongo cursor = db.restaurants.find().sort([ ("borough", pymongo.ASCENDING), ("address.zipcode", pymongo.ASCENDING) ])
1 """Query the database. 2 3 The `filter` argument is a prototype document that all results 4 must match. For example: 5 6 >>> db.test.find({"hello": "world"}) 7 8 only matches documents that have a key "hello" with value 9 "world". Matches can have other keys *in addition* to 10 "hello". The `projection` argument is used to specify a subset 11 of fields that should be included in the result documents. By 12 limiting results to a certain subset of fields you can cut 13 down on network traffic and decoding time. 14 15 Raises :class:`TypeError` if any of the arguments are of 16 improper type. Returns an instance of 17 :class:`~pymongo.cursor.Cursor` corresponding to this query. 18 19 The :meth:`find` method obeys the :attr:`read_preference` of 20 this :class:`Collection`. 21 22 :Parameters: 23 - `filter` (optional): a SON object specifying elements which 24 must be present for a document to be included in the 25 result set 26 - `projection` (optional): a list of field names that should be 27 returned in the result set or a dict specifying the fields 28 to include or exclude. If `projection` is a list "_id" will 29 always be returned. Use a dict to exclude fields from 30 the result (e.g. projection={'_id': False}). 31 - `skip` (optional): the number of documents to omit (from 32 the start of the result set) when returning the results 33 - `limit` (optional): the maximum number of results to 34 return 35 - `no_cursor_timeout` (optional): if False (the default), any 36 returned cursor is closed by the server after 10 minutes of 37 inactivity. If set to True, the returned cursor will never 38 time out on the server. Care should be taken to ensure that 39 cursors with no_cursor_timeout turned on are properly closed. 40 - `cursor_type` (optional): the type of cursor to return. The valid 41 options are defined by :class:`~pymongo.cursor.CursorType`: 42 43 - :attr:`~pymongo.cursor.CursorType.NON_TAILABLE` - the result of 44 this find call will return a standard cursor over the result set. 45 - :attr:`~pymongo.cursor.CursorType.TAILABLE` - the result of this 46 find call will be a tailable cursor - tailable cursors are only 47 for use with capped collections. They are not closed when the 48 last data is retrieved but are kept open and the cursor location 49 marks the final document position. If more data is received 50 iteration of the cursor will continue from the last document 51 received. For details, see the `tailable cursor documentation 52 <http://www.mongodb.org/display/DOCS/Tailable+Cursors>`_. 53 - :attr:`~pymongo.cursor.CursorType.TAILABLE_AWAIT` - the result 54 of this find call will be a tailable cursor with the await flag 55 set. The server will wait for a few seconds after returning the 56 full result set so that it can capture and return additional data 57 added during the query. 58 - :attr:`~pymongo.cursor.CursorType.EXHAUST` - the result of this 59 find call will be an exhaust cursor. MongoDB will stream batched 60 results to the client without waiting for the client to request 61 each batch, reducing latency. See notes on compatibility below. 62 63 - `sort` (optional): a list of (key, direction) pairs 64 specifying the sort order for this query. See 65 :meth:`~pymongo.cursor.Cursor.sort` for details. 66 - `allow_partial_results` (optional): if True, mongos will return 67 partial results if some shards are down instead of returning an 68 error. 69 - `oplog_replay` (optional): If True, set the oplogReplay query 70 flag. 71 - `batch_size` (optional): Limits the number of documents returned in 72 a single batch. 73 - `manipulate` (optional): **DEPRECATED** - If True (the default), 74 apply any outgoing SON manipulators before returning. 75 - `collation` (optional): An instance of 76 :class:`~pymongo.collation.Collation`. This option is only supported 77 on MongoDB 3.4 and above. 78 - `return_key` (optional): If True, return only the index keys in 79 each document. 80 - `show_record_id` (optional): If True, adds a field ``$recordId`` in 81 each document with the storage engine's internal record identifier. 82 - `snapshot` (optional): If True, prevents the cursor from returning 83 a document more than once because of an intervening write 84 operation. 85 - `hint` (optional): An index, in the same format as passed to 86 :meth:`~pymongo.collection.Collection.create_index` (e.g. 87 ``[('field', ASCENDING)]``). Pass this as an alternative to calling 88 :meth:`~pymongo.cursor.Cursor.hint` on the cursor to tell Mongo the 89 proper index to use for the query. 90 - `max_time_ms` (optional): Specifies a time limit for a query 91 operation. If the specified time is exceeded, the operation will be 92 aborted and :exc:`~pymongo.errors.ExecutionTimeout` is raised. Pass 93 this as an alternative to calling 94 :meth:`~pymongo.cursor.Cursor.max_time_ms` on the cursor. 95 - `max_scan` (optional): The maximum number of documents to scan. 96 Pass this as an alternative to calling 97 :meth:`~pymongo.cursor.Cursor.max_scan` on the cursor. 98 - `min` (optional): A list of field, limit pairs specifying the 99 inclusive lower bound for all keys of a specific index in order. 100 Pass this as an alternative to calling 101 :meth:`~pymongo.cursor.Cursor.min` on the cursor. 102 - `max` (optional): A list of field, limit pairs specifying the 103 exclusive upper bound for all keys of a specific index in order. 104 Pass this as an alternative to calling 105 :meth:`~pymongo.cursor.Cursor.max` on the cursor. 106 - `comment` (optional): A string or document. Pass this as an 107 alternative to calling :meth:`~pymongo.cursor.Cursor.comment` on the 108 cursor. 109 - `modifiers` (optional): **DEPRECATED** - A dict specifying 110 additional MongoDB query modifiers. Use the keyword arguments listed 111 above instead. 112 113 .. note:: There are a number of caveats to using 114 :attr:`~pymongo.cursor.CursorType.EXHAUST` as cursor_type: 115 116 - The `limit` option can not be used with an exhaust cursor. 117 118 - Exhaust cursors are not supported by mongos and can not be 119 used with a sharded cluster. 120 121 - A :class:`~pymongo.cursor.Cursor` instance created with the 122 :attr:`~pymongo.cursor.CursorType.EXHAUST` cursor_type requires an 123 exclusive :class:`~socket.socket` connection to MongoDB. If the 124 :class:`~pymongo.cursor.Cursor` is discarded without being 125 completely iterated the underlying :class:`~socket.socket` 126 connection will be closed and discarded without being returned to 127 the connection pool.
find_one_and_delete
def find_one_and_delete(self, filter, projection=None, sort=None, **kwargs): """Finds a single document and deletes it, returning the document. >>> db.test.count({'x': 1}) 2 >>> db.test.find_one_and_delete({'x': 1}) {u'x': 1, u'_id': ObjectId('54f4e12bfba5220aa4d6dee8')} >>> db.test.count({'x': 1}) 1 If multiple documents match *filter*, a *sort* can be applied. >>> for doc in db.test.find({'x': 1}): ... print(doc) ... {u'x': 1, u'_id': 0} {u'x': 1, u'_id': 1} {u'x': 1, u'_id': 2} >>> db.test.find_one_and_delete( ... {'x': 1}, sort=[('_id', pymongo.DESCENDING)]) {u'x': 1, u'_id': 2} The *projection* option can be used to limit the fields returned. >>> db.test.find_one_and_delete({'x': 1}, projection={'_id': False}) {u'x': 1} :Parameters: - `filter`: A query that matches the document to delete. - `projection` (optional): a list of field names that should be returned in the result document or a mapping specifying the fields to include or exclude. If `projection` is a list "_id" will always be returned. Use a mapping to exclude fields from the result (e.g. projection={'_id': False}). - `sort` (optional): a list of (key, direction) pairs specifying the sort order for the query. If multiple documents match the query, they are sorted and the first is deleted. - `**kwargs` (optional): additional command arguments can be passed as keyword arguments (for example maxTimeMS can be used with recent server versions).
find_one_and_replace
def find_one_and_replace(self, filter, replacement, projection=None, sort=None, upsert=False, return_document=ReturnDocument.BEFORE, **kwargs): """Finds a single document and replaces it, returning either the original or the replaced document. The :meth:`find_one_and_replace` method differs from :meth:`find_one_and_update` by replacing the document matched by *filter*, rather than modifying the existing document. >>> for doc in db.test.find({}): ... print(doc) ... {u'x': 1, u'_id': 0} {u'x': 1, u'_id': 1} {u'x': 1, u'_id': 2} >>> db.test.find_one_and_replace({'x': 1}, {'y': 1}) {u'x': 1, u'_id': 0} >>> for doc in db.test.find({}): ... print(doc) ... {u'y': 1, u'_id': 0} {u'x': 1, u'_id': 1} {u'x': 1, u'_id': 2} :Parameters: - `filter`: A query that matches the document to replace. - `replacement`: The replacement document. - `projection` (optional): A list of field names that should be returned in the result document or a mapping specifying the fields to include or exclude. If `projection` is a list "_id" will always be returned. Use a mapping to exclude fields from the result (e.g. projection={'_id': False}). - `sort` (optional): a list of (key, direction) pairs specifying the sort order for the query. If multiple documents match the query, they are sorted and the first is replaced. - `upsert` (optional): When ``True``, inserts a new document if no document matches the query. Defaults to ``False``. - `return_document`: If :attr:`ReturnDocument.BEFORE` (the default), returns the original document before it was replaced, or ``None`` if no document matches. If :attr:`ReturnDocument.AFTER`, returns the replaced or inserted document. - `**kwargs` (optional): additional command arguments can be passed as keyword arguments (for example maxTimeMS can be used with recent server versions).
find_one_and_update
def find_one_and_update(self, filter, update, projection=None, sort=None, upsert=False, return_document=ReturnDocument.BEFORE, **kwargs): """Finds a single document and updates it, returning either the original or the updated document. >>> db.test.find_one_and_update( ... {'_id': 665}, {'$inc': {'count': 1}, '$set': {'done': True}}) {u'_id': 665, u'done': False, u'count': 25}} By default :meth:`find_one_and_update` returns the original version of the document before the update was applied. To return the updated version of the document instead, use the *return_document* option. >>> from pymongo import ReturnDocument >>> db.example.find_one_and_update( ... {'_id': 'userid'}, ... {'$inc': {'seq': 1}}, ... return_document=ReturnDocument.AFTER) {u'_id': u'userid', u'seq': 1} You can limit the fields returned with the *projection* option. >>> db.example.find_one_and_update( ... {'_id': 'userid'}, ... {'$inc': {'seq': 1}}, ... projection={'seq': True, '_id': False}, ... return_document=ReturnDocument.AFTER) {u'seq': 2} The *upsert* option can be used to create the document if it doesn't already exist. >>> db.example.delete_many({}).deleted_count 1 >>> db.example.find_one_and_update( ... {'_id': 'userid'}, ... {'$inc': {'seq': 1}}, ... projection={'seq': True, '_id': False}, ... upsert=True, ... return_document=ReturnDocument.AFTER) {u'seq': 1} If multiple documents match *filter*, a *sort* can be applied. >>> for doc in db.test.find({'done': True}): ... print(doc) ... {u'_id': 665, u'done': True, u'result': {u'count': 26}} {u'_id': 701, u'done': True, u'result': {u'count': 17}} >>> db.test.find_one_and_update( ... {'done': True}, ... {'$set': {'final': True}}, ... sort=[('_id', pymongo.DESCENDING)]) {u'_id': 701, u'done': True, u'result': {u'count': 17}} :Parameters: - `filter`: A query that matches the document to update. - `update`: The update operations to apply. - `projection` (optional): A list of field names that should be returned in the result document or a mapping 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}). - `sort` (optional): a list of (key, direction) pairs specifying the sort order for the query. If multiple documents match the query, they are sorted and the first is updated. - `upsert` (optional): When ``True``, inserts a new document if no document matches the query. Defaults to ``False``. - `return_document`: If :attr:`ReturnDocument.BEFORE` (the default), returns the original document before it was updated, or ``None`` if no document matches. If :attr:`ReturnDocument.AFTER`, returns the updated or inserted document. - `**kwargs` (optional): additional command arguments can be passed as keyword arguments (for example maxTimeMS can be used with recent server versions).
3字段的一些操作(3层)
1.增加字段:collection.update({"_id":1},{"$set":{"new_field":0}}) #红色为查找条件,绿色为新增字段(当document中没有new_field这个字段时,则新增这个字段)
2.删除字段:collection.update({"_id":1},{"$unset":{"new_field":1}}) #红色为查找条件,绿色为删除字段
3.按条件查找:collection.find_one({"_id":1}) #红色为查找条件
collection.find({"_id":1})
4.统计不含有某一字段的记录数:dbName.collectionName.find({fieldName:null}).count()5.求某字段最大值:collection.find().sort({"_id":-1}).limit(1)#得到的记录为集合中"_id"值最大的那一条