Skip to content

Instantly share code, notes, and snippets.

@alexandre
Created April 24, 2016 04:24
Show Gist options
  • Save alexandre/00916dca96fe9d3f9bae5dba768f7321 to your computer and use it in GitHub Desktop.
Save alexandre/00916dca96fe9d3f9bae5dba768f7321 to your computer and use it in GitHub Desktop.

Algo que eu descobri durante um teste....

from pymongo import MongoClient


client = MongoClient()

db = client.test_database

expected_doc = {
    "name": "alexandre",
    "lang": "python3",
    "etc": "potato"
}

db["my_new_collection"].insert(doc_or_docs=expected_doc)

# _id -> False to return only the original doc value
try:
    assert db["my_new_collection"].find_one(expected_doc, {"_id": False}).keys() == expected_doc.keys()
    assert db["my_new_collection"].find_one(expected_doc, {"_id": False}).values() == expected_doc.values()
except AssertionError:
    print("Fail! Now your dict items is: ")
    print(expected_doc.items())

É...vai cair no except. Motivo:

https://github.com/mongodb/mongo-python-driver/blob/master/pymongo/collection.py#L628-L675

Uma seleção um pouco mais especifica:

https://github.com/mongodb/mongo-python-driver/blob/master/pymongo/collection.py#L671-L675

@alexandre
Copy link
Author

Versão simplificada:

 ~>python
Python 3.5.1 (default, Mar 11 2016, 05:18:44) 
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymongo import MongoClient
>>> client = MongoClient()
>>> db = client.test_database
>>> expected_doc = {
...     "name": "alexandre",
...     "lang": "python3",
...     "etc": "potato"
... }
>>> db["my_new_collection"].insert(doc_or_docs=expected_doc)
ObjectId('571c4ba0ed683349550e8588')
>>> expected_doc
{'_id': ObjectId('571c4ba0ed683349550e8588'), 'name': 'alexandre', 'lang': 'python3', 'etc': 'potato'}
>>> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment