{
"_id" : ObjectId("59e05fa63e0f5015dcaeadbe"),
"name" : "santu",
"mes": "Test Message 1",
"time" : ISODate("2017-10-13T06:39:34.273Z"),
"replay" : [
{
"time" : ISODate("2017-10-13T06:39:55.365Z"),
"_id" : ObjectId("59e05fbb3e0f5015dcaeadbf"),
"replayer" : "santu",
"replaymes": "Test Message Reply 1"
}
],
"__v" : 0
}
The data structure is like this. I want to get the number of embedded documents or all the contents under’ replay’
db.mesModel.aggregate([
{$project: {num: {$size: '$replay'}}}
])
The result of such query language printing is
[ { _id: 59e05fa63e0f5015dcaeadbe, replay: 1 },
{ _id: 59e063d85f465906606add9a, replay: 1 },
{ _id: 59e063da5f465906606add9b, replay: 1 },
{ _id: 59e063dd5f465906606add9c, replay: 1 } ]
The number of child data of all my parent data is printed out
The Chinese documents are not very complete. . . Looking at English is a little stressful.
The specific realization goal is to obtain all “replaymes” in the above data.
I wonder if there is an api that can be obtained directly.
My own implementation is to find the number of embedded data, traverse
db.mesModel.findOne({mes:'xxx'},(err,doc)=>{
var len = doc.replay.length
for(let i=0; i<len; i++){
The console.log (doc.replay [I]. replaymes)//has been printed out.
}
})
Accurate multi-criteria query of embedded documents, the realization goal is to find eligible replays under known replays by replays and time.
db.mesModel.aggregate(
{"$project":{"replay":"$replay"}},
{"$unwind":"$replay"},
{"$match":{"replay.replaymes":'xxx','replay.time':'xxx'}}
,function(err,doc){
console.log(doc)
})
If time is used as the condition, it cannot be matched, but if replay.replayer is used as the condition, it can be matched.
You don’t have to go through it, you can just get it.
replay
Thedb.mesModel.findOne({mes:'xxx'}, 'replay')
The following goal is to find eligible replays under known replays by replays and time.
It should be possibledb. mesModel.aggregate( { $match : {"replay.replaymes":'xxx','replay.time':'xxx'} }, 'replay' );