Mongoose, acquiring embedded documents and acquiring the number of embedded documents

  mongodb, question
{
 "_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, check the total number of such acquisitions

db.Scores.aggregate([
 {$project: {'cnt': {$size: '$Scores'}}}
 ])

And write like this

db.mesModel.aggregate([
 {$project: {num: {$size: '$replay'}}}
 ])

The result is

[ { _id: 59e05fa63e0f5015dcaeadbe, replay: 1 },
 { _id: 59e063d85f465906606add9a, replay: 1 },
 { _id: 59e063da5f465906606add9b, replay: 1 },
 { _id: 59e063dd5f465906606add9c, replay: 1 } ]

I printed everything, but all I need is the total number of embedded data under a single data.

The final data I want is replays under Replay. My method is

const mes = req.body.mes
 db.mesModel.findOne({mes:mes},(err,doc)={
 console.log(doc.replay[key].replaymes)
 })

https://docs.mongodb.com/manu …
https://docs.mongodb.com/manu …

I found you a document. ..

  • $project
    Passes along the documents with the requested fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.
  • $size
    Counts and returns the total the number of items in an array.

Aggregate has many operators, you can go through them one by one slowly. ..