Re: "projection=" doesn't work in pymongo.collection.Collection.find (pymongo 3.2.2)

From: Kevin Adistambha <kevinadi@xxxxxxxxxxx>
To: mongodb-user <mongodb-user@xxxxxxxxxxxxxxxx>
Date: Mon, 2 May 2016 23:04:10 -0700 (PDT)
Why ads?


Hi,

projection= [list] or {dict} will limit the output of find(), but in my 
script filter always works, but projection never works, no matter a list or 
a dict.

I don’t know how to set the projection to limit the output of find(). Who 
can give me an example?

For example, I have a collection named test which contains a single record:

db.test.find()
{
  "_id": ObjectId("5727f0f607cba93561357d79"),
  "a": 1,
  "b": 2,
  "c": 3,
  "d": 4,
  "e": 5
}

Using pymongo version 3.2.2, I can use the projection parameter to select 
the returned fields using an array of field names:

db = pymongo.MongoClient('localhost').test
list(db.test.find(projection=['a','b','c']))
[{u'_id': ObjectId('5727f0f607cba93561357d79'),
  u'a': 1.0,
  u'b': 2.0,
  u'c': 3.0}]

If you need to exclude fields instead, you need to use the dictionary form 
of projection:

list(db.test.find(projection={'_id':0}))
[{u'a': 1.0, u'b': 2.0, u'c': 3.0, u'd': 4.0, u'e': 5.0}]

Best regards,
Kevin


-- 
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
--- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@xxxxxxxxxxxxxxxx.
To post to this group, send email to mongodb-user@xxxxxxxxxxxxxxxx.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/ab717754-be67-4b47-b67a-c4dbd6c19da9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Why ads?