从mongoDB的3级嵌套模式中获取一个对象

时间:2019-03-31 10:49:00

标签: node.js mongodb mongoose

我有一个带有3级嵌套数组的架构,我需要从3级获得一个对象。 我不是在寻找在嵌套数组中找到_id的方法。我希望只检索有问题的_id
这是我的json响应

{
   "_id":"5c98bec0e7a4b7103855c875",
   "name":"Cooking Course",
   "units":[
      {
         "_id":"5c238s7a4bdsdfs55c8cdddd4r",
         "name":"making pizza",
         "videos":[
            {
               "_id":"5c98bec0e7a4b71038vcbn5",
               "title":"making a dough",
               "url":"www.example.com/3224",
               "views":"50"
            },
            {
               "_id":"5cadabse7fsda4b71fss32s",
               "title":"heating the oven",
               "url":"www.example.com/32ss3",
               "views":"300"
            }
         ]
      },
      {
         "_id":"5c238s7a4bdsdfs55c8c2342351wf",
         "name":"eating pizza",
         "videos":[
            {
               "_id":"5c98bec0e7a4b710334242cw",
               "title":"how to eat 101",
               "url":"www.example.com/3224",
               "views":"300"
            },
            {
               "_id":"5cadabse7fsda4bwerw32",
               "title":"how to eat 102",
               "url":"www.example.com/33d2",
               "views":"100"
            }
         ]
      },
      {
         "_id":"5c238s7a4bdsdfs55c657fxs",
         "name":"making burgers",
         "videos":[
            {
               "_id":"5c98bec0e7a4b71033245vv",
               "title":"buying the meat",
               "url":"www.example.com/3224",
               "views":"150"
            },
            {
               "_id":"5cadabse7fsda4b71034352",
               "title":"grill the meat",
               "url":"www.example.com/32ss3",
               "views":"300"
            }
         ]
      },
      {
         "_id":"5c238s7a4bdsdfs55c5435ds",
         "name":"eating burgers",
         "videos":[
            {
               "_id":"5c98bec0e7a4b710gfd45",
               "title":"how to eat 101",
               "url":"www.example.com/3224",
               "views":"70"
            },
            {
               "_id":"5cadabse7fsda4sadf2134",
               "title":"how to eat 102",
               "url":"www.example.com/33d2",
               "views":"150"
            }
         ]
      }
   ]
}

我要查找视频ID:“ 5cadabse7fsda4sadf2134” 我已经尝试过了:

Course.findOne(
    { "units.vidoes._id": req.params.vidoeId},
    {
      "vidoes.$": 1,
    }
  )
    .then(course => res.json(course ))
    .catch(err => res.status(404).json(err));

,但它返回包含视频以及该单元中所有其他视频的单元。 我只想要那个视频。

0 个答案:

没有答案