snake plissken
snake plissken

Reputation: 2669

PyMongo - Match by one nested field

I am wondering how is it possible to perform complex queries in PyMongo API in order. I've got a database and I want to retrieve data. Database feature are {id, username, ...}, id contains {uid, timestamp}. I want to perform queries checking the specific uid. I've tried something like:

db = client.test_database
db = client['trendSetters']
collection = db.test_collection
collection = db["cms_users_features"]
result =  collection.find_one()

for col in collection.find({"_id": {"uid": 702993}}):
    print col

EDIT

for col in collection.find({"_id": {"uid": 702993, "timestamp":1393554289813 }}):
    print col

If I add timestamp I can perform my query. How is it possible to do so, without adding timestamp?

Upvotes: 1

Views: 530

Answers (1)

vaultah
vaultah

Reputation: 46523

You can access the nested field by adding a dot between a parent field name and a nested field name: parent_field.child_field

This is the solution:

for col in collection.find({"_id.uid": 702993}):
    print col

Upvotes: 1

Related Questions