參考: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"值最大的那一條