Search code examples
pymongo

PyMongo query with projection


what is the PyMongo way for db.collection.find(criteria, projection).I cant find any doc for specifying projection to the collection.find() method


Solution

  • Is equeal to mongo shell, but the condition between ""

    db.foo.find({"field1.field2":123},{"field1":{"$elemMatch":{"field2":123}}})
    
    db.foo.update({"_id":3,"field1.field2":1},{"$set":{"field1.$.field2":2}})
    

    Or if you mean to projections to show any fields, is equal to mongo shell

    db.foo.find({"field1.field2":123},{"field1.field2":1, "_id":1})