Skip to content

Instantly share code, notes, and snippets.

@alexandre
Created April 24, 2016 04:24

Revisions

  1. alexandre created this gist Apr 24, 2016.
    33 changes: 33 additions & 0 deletions pymongo.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    Algo que eu descobri durante um teste....
    ```python
    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