Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyMongo query with projection

Tags:

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

like image 908
sajith Avatar asked Mar 31 '14 18:03

sajith


People also ask

How do I add a projection in MongoDB query?

Build a To-Do List App with Node, Express, React and MongoDB. In MongoDB, projection means selecting only the necessary data rather than selecting whole of the data of a document. If a document has 5 fields and you need to show only 3, then select only 3 fields from them.

What is a projection in MongoDB queries?

What are projection queries? In MongoDB, the default for queries is to return all fields in matching documents. A projection query is used to specify or restrict the data returned in query results. By specifying a projection query, you can specify the fields you want to return or exclude.

What are projection queries?

Projection queries allow you to query Datastore for just those specific properties of an entity that you actually need, at lower latency and cost than retrieving the entire entity. Projection queries are similar to SQL queries of the form: SELECT name, email, phone FROM CUSTOMER.

How do I display a selected field in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});


2 Answers

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})
like image 166
Carlos Rodriguez Avatar answered Oct 06 '22 10:10

Carlos Rodriguez


projection (optional): a list of field names that should be returned in the result set or a dict specifying the fields to include or exclude. If projection is a list “_id” will always be returned. Use a dict to exclude fields from the result (e.g. projection={‘_id’: False}).

Find more from here

like image 35
Shirantha Madusanka Avatar answered Oct 06 '22 09:10

Shirantha Madusanka