mongoose填充2个对象id

时间:2022-01-16 18:56:40

my current db looks like this

我当前的数据库看起来像这样

current db

    { 
    "_id" : ObjectId("5a6816bb8b38336ee1e842b4"), 
    "date" : ISODate("2018-01-26T00:00:00.000+0000"), 
    "examType" : ObjectId("5a65a84d13ea9dbfb52125df"), 
    "className" : "8", 
    "section" : "f", 
    "subject" : "Language", 
    "students" : [
        {
            "comments" : "good", 
            "marks" : NumberInt(100), 
            "studentId" : ObjectId("5a56fd3a02a50271d233a92f"), 
            "_id" : ObjectId("5a6821168b38336ee1e842ba")
        }, 
        {
            "comments" : "nice", 
            "marks" : NumberInt(100), 
            "studentId" : ObjectId("5a65ce5cc0533638e4e2df3f"), 
            "_id" : ObjectId("5a6821168b38336ee1e842b9")
        }
    ] 
}

i want to populate with two ids... one is examType and other is studentId in the students array of objects.

我想填充两个ID ...一个是examType,另一个是studentId在学生对象数组中。

my desired output is

我想要的输出是

expected output

{ 
   "_id" : ObjectId("5a6816bb8b38336ee1e842b4"), 
   "date" : ISODate("2018-01-26T00:00:00.000+0000"), 
   "examType" :  {
               "_id": "5a65a84d13ea9dbfb52125df",
               "name": "annual",
               "__v": 0,
               "totalMarks": 100
               }, 
    "className" : "8", 
    "section" : "f", 
    "subject" : "Language",
    "students" : [
           {
             "comments": "good",
             "marks": 100,
             "student": {
                       "_id": "5a56fd3a02a50271d233a92f",
                       "firstName": "test",
                       "lastName": "sample",
                       "dob": "2017-12-31T18:30:00.000Z",
                       },
                "_id" : ObjectId("5a6821168b38336ee1e842ba")
          }, 
         {
          "comments" : "nice", 
          "marks" : NumberInt(100), 
          "studentId" : {
                        "_id": "5a56fd3a02a50271d233a93f",
                        "firstName": "abcd",
                        "lastName": "efgh",
                        "dob": "2017-12-31T18:30:00.000Z",
                        }, 
                    "_id" : ObjectId("5a6821168b38336ee1e842b9")
                   }
                  ], 
         }

how can i achieve this multiple populate for the examType and studentId in the array of objects?

如何在对象数组中为examType和studentId实现这个多重填充?

and also need to group by with the subject

并且还需要与主题分组

1 个解决方案

#1


0  

If you're using Mongoose >= 3.6, you can pass a space delimited string of the path names to populate:

如果你使用的是Mongoose> = 3.6,你可以传递一个以空格分隔的路径名字符串来填充:

  <tablename>.find()
        .populate('examType  students.studentId')
        .exec(function (err, results) {});

http://mongoosejs.com/docs/populate.html

http://mongoosejs.com/docs/populate.html

#1


0  

If you're using Mongoose >= 3.6, you can pass a space delimited string of the path names to populate:

如果你使用的是Mongoose> = 3.6,你可以传递一个以空格分隔的路径名字符串来填充:

  <tablename>.find()
        .populate('examType  students.studentId')
        .exec(function (err, results) {});

http://mongoosejs.com/docs/populate.html

http://mongoosejs.com/docs/populate.html