热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

pymongo的一些操作

参考:http:www.yiibai.commongodbmongodb_drop_collection.htmlhttp:www.cnblogs.comzhouxuchenp

 参考: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``.
View Code

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``.
View Code

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.
View Code

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.
View Code

replace_one,替换,这个是没有操作符的

替换一个文档

要替换整个文档(除了_id字段),将一个完整的文档作为第二个参数传给update()方法。替代文档对应原来的文档可以有不同的字段。在替代文档中,你可以忽略_id字段因为它是不变的。如果你包含了_id字段,那它必须和原文档的值相同。

重要:
在更新之后,该文档将只包含替代文档的字段。

在如下的更新操作后,被修改的文档将只剩下_idnameaddress字段。该文档将不再包含restaurant_idcuisinegrades以及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.
View Code

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.
View Code

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.
View Code

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)
        """
View Code

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               `_.
 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.
View Code

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).
View Code

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).
View Code

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).
View Code

 

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"值最大的那一条

 


推荐阅读
  • Django框架下的对象关系映射(ORM)详解
    在Django框架中,对象关系映射(ORM)技术是解决面向对象编程与关系型数据库之间不兼容问题的关键工具。通过将数据库表结构映射到Python类,ORM使得开发者能够以面向对象的方式操作数据库,从而简化了数据访问和管理的复杂性。这种技术不仅提高了代码的可读性和可维护性,还增强了应用程序的灵活性和扩展性。 ... [详细]
  • 原标题:django中ImageField的使用---2020.12.19ImageField的使用笔记今天完善作业写的订单系统,主要是给每一个菜品增加 ... [详细]
  • 本文深入探讨了 MXOTDLL.dll 在 C# 环境中的应用与优化策略。针对近期公司从某生物技术供应商采购的指纹识别设备,该设备提供的 DLL 文件是用 C 语言编写的。为了更好地集成到现有的 C# 系统中,我们对原生的 C 语言 DLL 进行了封装,并利用 C# 的互操作性功能实现了高效调用。此外,文章还详细分析了在实际应用中可能遇到的性能瓶颈,并提出了一系列优化措施,以确保系统的稳定性和高效运行。 ... [详细]
  • Tornado硬件管理平台中的设备信息采集技术深入解析(三)
    深入解析 Tornado 硬件管理平台中的设备信息采集技术,本文聚焦于 `monitor.py` 脚本的关键字段分析。该脚本通过导入 `psutil`、`time` 和 `datetime` 模块,以及使用 `pprint` 进行数据格式化输出,实现对系统资源和设备状态的高效监控与数据采集。 ... [详细]
  • Django信号使得某个操作之前能定制化一些任务-内置信号-导入fromdjango.core.signalsimportXX00-注册函数-自定义-自定义-定义信号importd ... [详细]
  • FBV代表func,也就是函数。CBV代表class,也就是类应用场景:登录验证.FBV写法:deflogin(request):dic{tag:N ... [详细]
  • 背景由于性能数据每天导入量,数据库表空间每天增长很快,且不需要太长的保存周期,为避免爆表,因此需要定制定期清理计划。数据的清理可以有多种方案,根据场景的不同可以分为离线,在线。后续 ... [详细]
  • 本项目在Java Maven框架下,利用POI库实现了Excel数据的高效导入与导出功能。通过优化数据处理流程,提升了数据操作的性能和稳定性。项目已发布至GitHub,当前最新版本为0.0.5。该项目不仅适用于小型应用,也可扩展用于大型企业级系统,提供了灵活的数据管理解决方案。GitHub地址:https://github.com/83945105/holygrail,Maven坐标:`com.github.83945105:holygrail:0.0.5`。 ... [详细]
  • 本题库精选了Java核心知识点的练习题,旨在帮助学习者巩固和检验对Java理论基础的掌握。其中,选择题部分涵盖了访问控制权限等关键概念,例如,Java语言中仅允许子类或同一包内的类访问的访问权限为protected。此外,题库还包括其他重要知识点,如异常处理、多线程、集合框架等,全面覆盖Java编程的核心内容。 ... [详细]
  • HBase在金融大数据迁移中的应用与挑战
    随着最后一台设备的下线,标志着超过10PB的HBase数据迁移项目顺利完成。目前,新的集群已在新机房稳定运行超过两个月,监控数据显示,新集群的查询响应时间显著降低,系统稳定性大幅提升。此外,数据消费的波动也变得更加平滑,整体性能得到了显著优化。 ... [详细]
  • 在 Android 开发中,通过合理利用系统通知服务,可以显著提升应用的用户交互体验。针对 Android 8.0 及以上版本,开发者需首先创建并注册通知渠道。本文将详细介绍如何在应用中实现这一功能,包括初始化通知管理器、创建通知渠道以及发送通知的具体步骤,帮助开发者更好地理解和应用这些技术细节。 ... [详细]
  • 在处理大规模并发请求时,传统的多线程或多进程模型往往无法有效解决性能瓶颈问题。尽管它们在处理小规模任务时能提升效率,但在高并发场景下,系统资源的过度消耗和上下文切换的开销会显著降低整体性能。相比之下,Python 的 `asyncio` 模块通过协程提供了一种轻量级且高效的并发解决方案。本文将深入解析 `asyncio` 模块的原理及其在实际应用中的优化技巧,帮助开发者更好地利用协程技术提升程序性能。 ... [详细]
  • Django 学习笔记(三)
    在模板文件中,还能嵌套入模板标签,做一些特殊处理,例如流程控制,下面将简单介绍下模板标签,主要介绍if和for ... [详细]
  • 参照网上教程,自定义标签过滤器templatestag/custom_markdown.py ... [详细]
  • python flask面试题_Python面试的50个经典问答,助你从容通过面试(下)
    26)Python中的局部变量和全局变量的使用规则是什么?局部变量:仅可在某个对象或函数内部使用,无法被其他对象或函数所引 ... [详细]
author-avatar
鸿少爷仰望星空
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有