Quantcast
Channel: Retrieve only the queried element in an object array in MongoDB collection - Stack Overflow
Browsing latest articles
Browse All 21 View Live

Answer by Stannum for Retrieve only the queried element in an object array in...

db.collection.aggregate([ {"$unwind": "$shapes" }, {"$match": {"$and": [ {"shapes.color": "red" }, {"shapes.shape": "circle" } ] } }, //remove objectid {"$project": { _id: 0 } } ])

View Article



Answer by yves amsellem for Retrieve only the queried element in an object...

If you want to return the first matching shapes, find() will do the trick (as detailed here), but if you want to return every matching shapes with the original document you can do the following...

View Article

Answer by TKirishima for Retrieve only the queried element in an object array...

For the new version of MongoDB, it's slightly different.For db.collection.find you can use the second parameter of find with the key being projectiondb.collection.find({}, {projection: {name: 1, email:...

View Article

Answer by Gouranga Satapathy for Retrieve only the queried element in an...

for more details refer =mongo db official referancesuppose you have document like this (you can have multiple document too) - {"_id": {"$oid": "63b5cfbfbcc3196a2a23c44b" },"results": [...

View Article

Answer by cprcrack for Retrieve only the queried element in an object array...

This answer does not fully answer the question but it's related and I'm writing it down because someone decided to close another question marking this one as duplicate (which is not).In my case I only...

View Article


Answer by Rafiq for Retrieve only the queried element in an object array in...

if you want to do filter, set and find at the same time.let post = await Post.findOneAndUpdate( { _id: req.params.id, tasks: { $elemMatch: { id: req.params.jobId, date, }, }, }, { $set:...

View Article

Answer by Ali for Retrieve only the queried element in an object array in...

Although the question was asked 9.6 years ago, this has been of immense help to numerous people, me being one of them. Thank you everyone for all your queries, hints and answers. Picking up from one of...

View Article

Answer by ashishSober for Retrieve only the queried element in an object...

Likewise you can find for the multipledb.getCollection('localData').aggregate([ // Get just the docs that contain a shapes element where color is 'red' {$match: {'shapes.color': {$in : ['red','yellow']...

View Article


Answer by KARTHIKEYAN.A for Retrieve only the queried element in an object...

Use aggregation function and $project to get specific object field in documentdb.getCollection('geolocations').aggregate([ { $project : { geolocation : 1} } ])result:{"_id" :...

View Article


Answer by Poonam Agrawal for Retrieve only the queried element in an object...

db.test.find( {"shapes.color": "red"}, {_id: 0})

View Article

Answer by Viral Patel for Retrieve only the queried element in an object...

db.getCollection('aj').find({"shapes.color":"red"},{"shapes.$":1})OUTPUTS{"shapes" : [ {"shape" : "circle","color" : "red" } ]}

View Article

Answer by Vaibhav Patil for Retrieve only the queried element in an object...

You just need to run querydb.test.find({"shapes.color": "red"}, {shapes: {$elemMatch: {color: "red"}}});output of this query is{"_id" : ObjectId("562e7c594c12942f08fe4192"),"shapes" : [ {"shape" :...

View Article

Answer by Narendran for Retrieve only the queried element in an object array...

Better you can query in matching array element using $slice is it helpful to returning the significant object in an array.db.test.find({"shapes.color" : "blue"}, {"shapes.$" : 1})$slice is helpful when...

View Article


Answer by anvarik for Retrieve only the queried element in an object array in...

Another interesing way is to use $redact, which is one of the new aggregation features of MongoDB 2.6. If you are using 2.6, you don't need an $unwind which might cause you performance problems if you...

View Article

Answer by Eddy for Retrieve only the queried element in an object array in...

Thanks to JohnnyHK.Here I just want to add some more complex usage.// Document { "_id" : 1"shapes" : [ {"shape" : "square", "color" : "red"}, {"shape" : "circle", "color" : "green"} ] } { "_id" :...

View Article


Answer by Vicky for Retrieve only the queried element in an object array in...

The syntax for find in mongodb is db.<collection name>.find(query, projection);and the second query that you have written, that is db.test.find( {shapes: {"$elemMatch": {color: "red"}}},...

View Article

Answer by shakthydoss for Retrieve only the queried element in an object...

Along with $project it will be more appropriate other wise matching elements will be clubbed together with other elements in document.db.test.aggregate( { "$unwind" : "$shapes" }, { "$match" : {...

View Article


Answer by JohnnyHK for Retrieve only the queried element in an object array...

MongoDB 2.2's new $elemMatch projection operator provides another way to alter the returned document to contain only the first matched shapes element:db.test.find( {"shapes.color": "red"}, {_id: 0,...

View Article

Answer by Stennie for Retrieve only the queried element in an object array in...

The new Aggregation Framework in MongoDB 2.2+ provides an alternative to Map/Reduce. The $unwind operator can be used to separate your shapes array into a stream of documents that can be...

View Article

Answer by Niels van der Rest for Retrieve only the queried element in an...

Caution: This answer provides a solution that was relevant at that time, before the new features of MongoDB 2.2 and up were introduced. See the other answers if you are using a more recent version of...

View Article

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" } ]},{...

View Article

Browsing latest articles
Browse All 21 View Live




Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>