Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bson/raw_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
>>> client.drop_database("db")
>>> client.drop_database("replica_db")
>>> db = client.db
>>> result = db.test.insert_many(
>>> result = db.coll.insert_many(
... [{"_id": 1, "a": 1}, {"_id": 2, "b": 1}, {"_id": 3, "c": 1}, {"_id": 4, "d": 1}]
... )
>>> replica_db = client.replica_db
>>> for doc in db.test.find():
>>> for doc in db.coll.find():
... print(f"raw document: {doc.raw}")
... print(f"decoded document: {bson.decode(doc.raw)}")
... result = replica_db.test.insert_one(doc)
... result = replica_db.coll.insert_one(doc)
...
raw document: b'...'
decoded document: {'_id': 1, 'a': 1}
Expand Down
2 changes: 1 addition & 1 deletion pymongo/_csot.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class _TimeoutContext(AbstractContextManager[Any]):
Use :func:`pymongo.timeout` instead::

with pymongo.timeout(0.5):
client.test.test.insert_one({})
client.db.coll.insert_one({})
"""

def __init__(self, timeout: Optional[float]):
Expand Down
112 changes: 56 additions & 56 deletions pymongo/asynchronous/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ async def bulk_write(
:class:`~pymongo.operations.DeleteOne`, or
:class:`~pymongo.operations.DeleteMany`).

>>> async for doc in db.test.find({}):
>>> async for doc in db.coll.find({}):
... print(doc)
...
{'x': 1, '_id': ObjectId('54f62e60fba5226811f634ef')}
Expand All @@ -725,7 +725,7 @@ async def bulk_write(
>>> from pymongo import InsertOne, DeleteOne, ReplaceOne
>>> requests = [InsertOne({'y': 1}), DeleteOne({'x': 1}),
... ReplaceOne({'w': 1}, {'z': 1}, upsert=True)]
>>> result = await db.test.bulk_write(requests)
>>> result = await db.coll.bulk_write(requests)
>>> result.inserted_count
1
>>> result.deleted_count
Expand All @@ -734,7 +734,7 @@ async def bulk_write(
0
>>> result.upserted_ids
{2: ObjectId('54f62ee28891e756a6e1abd5')}
>>> async for doc in db.test.find({}):
>>> async for doc in db.coll.find({}):
... print(doc)
...
{'x': 1, '_id': ObjectId('54f62e60fba5226811f634f0')}
Expand Down Expand Up @@ -845,12 +845,12 @@ async def insert_one(
) -> InsertOneResult:
"""Insert a single document.

>>> await db.test.count_documents({'x': 1})
>>> await db.coll.count_documents({'x': 1})
0
>>> result = await db.test.insert_one({'x': 1})
>>> result = await db.coll.insert_one({'x': 1})
>>> result.inserted_id
ObjectId('54f112defba522406c9cc208')
>>> await db.test.find_one({'x': 1})
>>> await db.coll.find_one({'x': 1})
{'x': 1, '_id': ObjectId('54f112defba522406c9cc208')}

:param document: The document to insert. Must be a mutable mapping
Expand Down Expand Up @@ -911,12 +911,12 @@ async def insert_many(
) -> InsertManyResult:
"""Insert an iterable of documents.

>>> await db.test.count_documents({})
>>> await db.coll.count_documents({})
0
>>> result = await db.test.insert_many([{'x': i} for i in range(2)])
>>> result = await db.coll.insert_many([{'x': i} for i in range(2)])
>>> await result.inserted_ids
[ObjectId('54f113fffba522406c9cc20e'), ObjectId('54f113fffba522406c9cc20f')]
>>> await db.test.count_documents({})
>>> await db.coll.count_documents({})
2

:param documents: A iterable of documents to insert.
Expand Down Expand Up @@ -1137,31 +1137,31 @@ async def replace_one(
) -> UpdateResult:
"""Replace a single document matching the filter.

>>> async for doc in db.test.find({}):
>>> async for doc in db.coll.find({}):
... print(doc)
...
{'x': 1, '_id': ObjectId('54f4c5befba5220aa4d6dee7')}
>>> result = await db.test.replace_one({'x': 1}, {'y': 1})
>>> result = await db.coll.replace_one({'x': 1}, {'y': 1})
>>> result.matched_count
1
>>> result.modified_count
1
>>> async for doc in db.test.find({}):
>>> async for doc in db.coll.find({}):
... print(doc)
...
{'y': 1, '_id': ObjectId('54f4c5befba5220aa4d6dee7')}

The *upsert* option can be used to insert a new document if a matching
document does not exist.

>>> result = await db.test.replace_one({'x': 1}, {'x': 1}, True)
>>> result = await db.coll.replace_one({'x': 1}, {'x': 1}, True)
>>> result.matched_count
0
>>> result.modified_count
0
>>> result.upserted_id
ObjectId('54f11e5c8891e756a6e1abd4')
>>> await db.test.find_one({'x': 1})
>>> await db.coll.find_one({'x': 1})
{'x': 1, '_id': ObjectId('54f11e5c8891e756a6e1abd4')}

:param filter: A query that matches the document to replace.
Expand Down Expand Up @@ -1247,18 +1247,18 @@ async def update_one(
) -> UpdateResult:
"""Update a single document matching the filter.

>>> async for doc in db.test.find():
>>> async for doc in db.coll.find():
... print(doc)
...
{'x': 1, '_id': 0}
{'x': 1, '_id': 1}
{'x': 1, '_id': 2}
>>> result = await db.test.update_one({'x': 1}, {'$inc': {'x': 3}})
>>> result = await db.coll.update_one({'x': 1}, {'$inc': {'x': 3}})
>>> result.matched_count
1
>>> result.modified_count
1
>>> async for doc in db.test.find():
>>> async for doc in db.coll.find():
... print(doc)
...
{'x': 4, '_id': 0}
Expand All @@ -1268,14 +1268,14 @@ async def update_one(
If ``upsert=True`` and no documents match the filter, create a
new document based on the filter criteria and update modifications.

>>> result = await db.test.update_one({'x': -10}, {'$inc': {'x': 3}}, upsert=True)
>>> result = await db.coll.update_one({'x': -10}, {'$inc': {'x': 3}}, upsert=True)
>>> result.matched_count
0
>>> result.modified_count
0
>>> result.upserted_id
ObjectId('626a678eeaa80587d4bb3fb7')
>>> await db.test.find_one(result.upserted_id)
>>> await db.coll.find_one(result.upserted_id)
{'_id': ObjectId('626a678eeaa80587d4bb3fb7'), 'x': -7}

:param filter: A query that matches the document to update.
Expand Down Expand Up @@ -1366,18 +1366,18 @@ async def update_many(
) -> UpdateResult:
"""Update one or more documents that match the filter.

>>> async for doc in db.test.find():
>>> async for doc in db.coll.find():
... print(doc)
...
{'x': 1, '_id': 0}
{'x': 1, '_id': 1}
{'x': 1, '_id': 2}
>>> result = await db.test.update_many({'x': 1}, {'$inc': {'x': 3}})
>>> result = await db.coll.update_many({'x': 1}, {'$inc': {'x': 3}})
>>> result.matched_count
3
>>> result.modified_count
3
>>> async for doc in db.test.find():
>>> async for doc in db.coll.find():
... print(doc)
...
{'x': 4, '_id': 0}
Expand Down Expand Up @@ -1602,12 +1602,12 @@ async def delete_one(
) -> DeleteResult:
"""Delete a single document matching the filter.

>>> await db.test.count_documents({'x': 1})
>>> await db.coll.count_documents({'x': 1})
3
>>> result = await db.test.delete_one({'x': 1})
>>> result = await db.coll.delete_one({'x': 1})
>>> result.deleted_count
1
>>> await db.test.count_documents({'x': 1})
>>> await db.coll.count_documents({'x': 1})
2

:param filter: A query that matches the document to delete.
Expand Down Expand Up @@ -1667,12 +1667,12 @@ async def delete_many(
) -> DeleteResult:
"""Delete one or more documents matching the filter.

>>> await db.test.count_documents({'x': 1})
>>> await db.coll.count_documents({'x': 1})
3
>>> result = await db.test.delete_many({'x': 1})
>>> result = await db.coll.delete_many({'x': 1})
>>> result.deleted_count
3
>>> await db.test.count_documents({'x': 1})
>>> await db.coll.count_documents({'x': 1})
0

:param filter: A query that matches the documents to delete.
Expand Down Expand Up @@ -1762,7 +1762,7 @@ def find(self, *args: Any, **kwargs: Any) -> AsyncCursor[_DocumentType]:
The `filter` argument is a query document that all results
must match. For example:

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

only matches documents that have a key "hello" with value
"world". Matches can have other keys *in addition* to
Expand Down Expand Up @@ -1965,7 +1965,7 @@ def find_raw_batches(self, *args: Any, **kwargs: Any) -> AsyncRawBatchCursor[_Do
:mod:`bson` module.

>>> import bson
>>> cursor = db.test.find_raw_batches()
>>> cursor = db.coll.find_raw_batches()
>>> async for batch in cursor:
... print(bson.decode_all(batch))

Expand Down Expand Up @@ -2191,7 +2191,7 @@ async def create_indexes(
>>> index1 = IndexModel([("hello", DESCENDING),
... ("world", ASCENDING)], name="hello_world")
>>> index2 = IndexModel([("goodbye", DESCENDING)])
>>> await db.test.create_indexes([index1, index2])
>>> await db.coll.create_indexes([index1, index2])
["hello_world", "goodbye_-1"]

:param indexes: A list of :class:`~pymongo.operations.IndexModel`
Expand Down Expand Up @@ -2517,7 +2517,7 @@ async def list_indexes(
) -> AsyncCommandCursor[MutableMapping[str, Any]]:
"""Get a cursor over the index documents for this collection.

>>> async for index in await db.test.list_indexes():
>>> async for index in await db.coll.list_indexes():
... print(index)
...
SON([('v', 2), ('key', SON([('_id', 1)])), ('name', '_id_')])
Expand Down Expand Up @@ -2612,9 +2612,9 @@ async def index_information(
``"name"`` keys, which are cleaned. Example output might look
like this:

>>> await db.test.create_index("x", unique=True)
>>> await db.coll.create_index("x", unique=True)
'x_1'
>>> await db.test.index_information()
>>> await db.coll.index_information()
{'_id_': {'key': [('_id', 1)]},
'x_1': {'unique': True, 'key': [('x', 1)]}}

Expand Down Expand Up @@ -3070,7 +3070,7 @@ async def aggregate_raw_batches(
:mod:`bson` module.

>>> import bson
>>> cursor = await db.test.aggregate_raw_batches([
>>> cursor = await db.coll.aggregate_raw_batches([
... {'$project': {'x': {'$multiply': [2, '$x']}}}])
>>> async for batch in cursor:
... print(bson.decode_all(batch))
Expand Down Expand Up @@ -3345,32 +3345,32 @@ async def find_one_and_delete(
) -> Optional[_DocumentType]:
"""Finds a single document and deletes it, returning the document.

>>> await db.test.count_documents({'x': 1})
>>> await db.coll.count_documents({'x': 1})
2
>>> await db.test.find_one_and_delete({'x': 1})
>>> await db.coll.find_one_and_delete({'x': 1})
{'x': 1, '_id': ObjectId('54f4e12bfba5220aa4d6dee8')}
>>> await db.test.count_documents({'x': 1})
>>> await db.coll.count_documents({'x': 1})
1

Returns ``None`` if no document matches the filter.

>>> await db.test.find_one_and_delete({'_exists': False})
>>> await db.coll.find_one_and_delete({'_exists': False})

If multiple documents match *filter*, a *sort* can be applied.

>>> async for doc in db.test.find({'x': 1}):
>>> async for doc in db.coll.find({'x': 1}):
... print(doc)
...
{'x': 1, '_id': 0}
{'x': 1, '_id': 1}
{'x': 1, '_id': 2}
>>> await db.test.find_one_and_delete(
>>> await db.coll.find_one_and_delete(
... {'x': 1}, sort=[('_id', pymongo.DESCENDING)])
{'x': 1, '_id': 2}

The *projection* option can be used to limit the fields returned.

>>> await db.test.find_one_and_delete({'x': 1}, projection={'_id': False})
>>> await db.coll.find_one_and_delete({'x': 1}, projection={'_id': False})
{'x': 1}

:param filter: A query that matches the document to delete.
Expand Down Expand Up @@ -3442,31 +3442,31 @@ async def find_one_and_replace(
"""Finds a single document and replaces it, returning either the
original or the replaced document.

>>> await db.test.find_one({'x': 1})
>>> await db.coll.find_one({'x': 1})
{'_id': 0, 'x': 1}
>>> await db.test.find_one_and_replace({'x': 1}, {'y': 2})
>>> await db.coll.find_one_and_replace({'x': 1}, {'y': 2})
{'_id': 0, 'x': 1}
>>> await db.test.find_one({'x': 1})
>>> await db.test.find_one({'y': 2})
>>> await db.coll.find_one({'x': 1})
>>> await db.coll.find_one({'y': 2})
{'_id': 0, 'y': 2}

Returns ``None`` if no document matches the filter.

>>> await db.test.find_one_and_replace({'_exists': False}, {'x': 1})
>>> await db.coll.find_one_and_replace({'_exists': False}, {'x': 1})

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.

>>> async for doc in db.test.find({}):
>>> async for doc in db.coll.find({}):
... print(doc)
...
{'x': 1, '_id': 0}
{'x': 1, '_id': 1}
{'x': 1, '_id': 2}
>>> await db.test.find_one_and_replace({'x': 1}, {'y': 1})
>>> await db.coll.find_one_and_replace({'x': 1}, {'y': 1})
{'x': 1, '_id': 0}
>>> async for doc in db.test.find({}):
>>> async for doc in db.coll.find({}):
... print(doc)
...
{'y': 1, '_id': 0}
Expand Down Expand Up @@ -3562,17 +3562,17 @@ async def find_one_and_update(
"""Finds a single document and updates it, returning either the
original or the updated document.

>>> await db.test.find_one({'_id': 665})
>>> await db.coll.find_one({'_id': 665})
{'_id': 665, 'done': False, 'count': 25}
>>> await db.test.find_one_and_update(
>>> await db.coll.find_one_and_update(
... {'_id': 665}, {'$inc': {'count': 1}, '$set': {'done': True}})
{'_id': 665, 'done': False, 'count': 25}
>>> await db.test.find_one({'_id': 665})
>>> await db.coll.find_one({'_id': 665})
{'_id': 665, 'done': True, 'count': 26}

Returns ``None`` if no document matches the filter.

>>> await db.test.find_one_and_update(
>>> await db.coll.find_one_and_update(
... {'_exists': False}, {'$inc': {'count': 1}})

When the filter matches, by default :meth:`find_one_and_update`
Expand Down Expand Up @@ -3612,12 +3612,12 @@ async def find_one_and_update(

If multiple documents match *filter*, a *sort* can be applied.

>>> async for doc in db.test.find({'done': True}):
>>> async for doc in db.coll.find({'done': True}):
... print(doc)
...
{'_id': 665, 'done': True, 'result': {'count': 26}}
{'_id': 701, 'done': True, 'result': {'count': 17}}
>>> await db.test.find_one_and_update(
>>> await db.coll.find_one_and_update(
... {'done': True},
... {'$set': {'final': True}},
... sort=[('_id', pymongo.DESCENDING)])
Expand Down
Loading