|
19 | 19 |
|
20 | 20 | Example: Moving a document between different databases/collections
|
21 | 21 |
|
22 |
| -.. testsetup:: |
23 |
| - from pymongo import MongoClient |
24 |
| - client = MongoClient(document_class=RawBSONDocument) |
25 |
| - client.drop_database('db') |
26 |
| - client.drop_database('replica_db') |
27 |
| -
|
28 | 22 | .. doctest::
|
29 | 23 |
|
30 | 24 | >>> import bson
|
31 | 25 | >>> from pymongo import MongoClient
|
32 | 26 | >>> from bson.raw_bson import RawBSONDocument
|
33 | 27 | >>> client = MongoClient(document_class=RawBSONDocument)
|
| 28 | + >>> client.drop_database('db') |
| 29 | + >>> client.drop_database('replica_db') |
34 | 30 | >>> db = client.db
|
35 | 31 | >>> result = db.test.insert_many([{'a': 1},
|
36 | 32 | ... {'b': 1},
|
37 | 33 | ... {'c': 1},
|
38 | 34 | ... {'d': 1}])
|
39 | 35 | >>> replica_db = client.replica_db
|
40 | 36 | >>> for doc in db.test.find():
|
41 |
| - ... print(f"raw document: {doc.raw}") |
42 |
| - ... print(f"decoded document: {bson.decode(doc.raw)}") |
| 37 | + ... print("raw document: %r" % (doc.raw,)) |
43 | 38 | ... result = replica_db.test.insert_one(doc)
|
44 |
| - raw document: b'...' |
45 |
| - decoded document: {'_id': ObjectId('...'), 'a': 1} |
46 |
| - raw document: b'...' |
47 |
| - decoded document: {'_id': ObjectId('...'), 'b': 1} |
48 |
| - raw document: b'...' |
49 |
| - decoded document: {'_id': ObjectId('...'), 'c': 1} |
50 |
| - raw document: b'...' |
51 |
| - decoded document: {'_id': ObjectId('...'), 'd': 1} |
| 39 | + raw document: '...' |
| 40 | + raw document: '...' |
| 41 | + raw document: '...' |
| 42 | + raw document: '...' |
| 43 | + >>> for doc in replica_db.test.find(projection={'_id': 0}): |
| 44 | + ... print("decoded document: %r" % (bson.decode(doc.raw),)) |
| 45 | + decoded document: {u'a': 1} |
| 46 | + decoded document: {u'b': 1} |
| 47 | + decoded document: {u'c': 1} |
| 48 | + decoded document: {u'd': 1} |
52 | 49 |
|
53 | 50 | For use cases like moving documents across different databases or writing binary
|
54 | 51 | blobs to disk, using raw BSON documents provides better speed and avoids the
|
|
0 commit comments