How to select a single field for all documents in a MongoDB collection?

In my MongoDB, I have a student collection with 10 records having fields name and roll. One record of this collection is: { “_id” : ObjectId(“53d9feff55d6b4dd1171dd9e”), “name” : “Swati”, “roll” : “80”, } I want to retrieve the field roll only for all 10 records in the collection as we would do in traditional database … Read more

Retrieve only the queried element in an object array in MongoDB collection

Suppose you have the following documents in my collection: { “_id”:ObjectId(“562e7c594c12942f08fe4192”), “shapes”:[ { “shape”:”square”, “color”:”blue” }, { “shape”:”circle”, “color”:”red” } ] }, { “_id”:ObjectId(“562e7c594c12942f08fe4193”), “shapes”:[ { “shape”:”square”, “color”:”black” }, { “shape”:”circle”, “color”:”green” } ] } Do query: db.test.find({“shapes.color”: “red”}, {“shapes.color”: 1}) Or db.test.find({shapes: {“$elemMatch”: {color: “red”}}}, {“shapes.color”: 1}) Returns matched document (Document 1), but always … Read more