I have a document
called Mapping
, which has an _id
and an array of objects called Mappings. I have another collection
called NewMappings
. For each _id
in NewMappings
, I need to search within the array of Mappings (of Mapping collection)
and return the _id
of Mapping
.
I wrote something like this, but it failed to return anything.
var d=db.NewMappings.find();
d.forEach(function(item){
db.matching.find({Mappings: {$elemMatch : {TargetId: item._id}}})
})
however, this query returned values
var d=db.NewMappings.find();
db.matching.find({Mappings: {$elemMatch : {TargetId: d[0]._id}}})
Am I missing something? Please help me. I am in dark. thanks in advance.
One way, if you just want to see the results is:
var d=db.NewMappings.find();
d.forEach(function(item){
db.matching.find({Mappings: {$elemMatch : {TargetId: item._id}}}).forEach(printjson)
})
You can also use the aggregation framework
Sounds like you're trying to do a Join, which isn't explicitly supported in MongoDB. You'll need to either use a few aggregation functions to flatten the data or a mapreduce.
Here's an example using mapreduce to restructure information: http://cookbook.mongodb.org/patterns/pivot/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.