是否可以 return 多个带有 mongoDB 和 Node.js 的子文档?

Is it possible to return multiple subdocuments with mongoDB and Node.js?

我目前有一个目前看起来像的模式:

var User = new Schema({
    id: String,
    firstName: String,
    lastName: String,
    password: String,
    username: String,
    position: [{
          title: String,
          location: String,
          start: String,
          term:Number,
          description:String,
                    date: {type: Date, default: Date.now}
  }]

});

我有两个用户,每个用户都有两个嵌入的位置文档。

用户 1:

"position" : [ 
    {
        "title" : "Web Developer",
        "location" : "Dublin",
        "start" : "May 2017",
        "term" : 6,
        "description" : " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis erat vitae dsit amet, consectetur adipiscing elit. Vivamus quis erat vitae dolor tempus euismod non in mi",
        "_id" : ObjectId("58d6b7e11e793c9a506ffe0f")
    }, 
    {
        "description" : "description",
        "term" : 12,
        "start" : "may 2018",
        "location" : "Dublin",
        "title" : "Web Developer",
        "_id" : ObjectId("58d6af99e4318f4703ceb2af")
    }
],

用户 2:

 "position" : [ 
        {
            "title" : "Software Engineer",
            "location" : "Cork",
            "start" : "May 2017",
            "term" : 9,
            "description" : " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis erat vitae dolor tempus euismod non in miorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis erat vitae dolor tempus euismod non in mi",
 "_id" : ObjectId("58d6af99e4318f4703cebsju7")

        }, 
        {
            "title" : "Web Developer",
            "location" : "Waterford",
            "start" : "May 2017",
            "term" : 6,
            "description" : " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis erat vitae dsit amet, consectetur adipiscing elit. Vivamus quis erat vitae dolor tempus euismod non in mi",
 "_id" : ObjectId("58d6af99e4318f4703ceb6aj")
        }
    ],

我的查询如下:

  app.post('/search', function (req, res) {

  var position = new RegExp(req.body.position, 'i');

  User.find({'position.title': position}, 'position.$').exec(function (err, result) {
      console.log(result);
      res.send({ results: result });
  });    //


});

当搜索 'Web Developer' 时,这将 return 第一个用户中的第一个 'Web Developer' 条目,以及第二个用户中的 'Web Developer' 条目,但我似乎无法return 任何后续条目,我只是想知道这是一个 MongoDB 问题,我只能 return 每个用户匹配的一个子文档吗?还是我的代码有问题?

下面可以看到returned对象,results对象return是两个用户,每个用户有一个position对象,不过应该是3个。

假设我在 positions 数组中有一个数组,我将如何访问它?

我当前的代码:

  app.get('/applied', function(req, res){
  User.aggregate(
        {$unwind : "$position"},
        {$unwind : "$position.applied"},
        {$match:{'position.applied.candidate_id': "58dc2bd4e7208a3ea143959e"}}).exec(function (err, result) {
          console.log(result);
        });
        res.render('applied', { title: 'applied'});
});

我的架构:

 position: [{
          title: String,
          location: String,
          start: String,
          term:Number,
          description:String,
          date: {type: Date, default: Date.now},
          applied:[{
                candidate_id: String,
                name: String
            }],
  }],

您可以为此使用 $unwind,它会从集合中获取多个条目,这与元素匹配或“.”不同。 return 仅首先匹配数据

的运算符
User.aggregate(
        {$unwind : "$position"},
        {$match:{'position.title': position}}).exec(function (err, result) {
      console.log(result);
      res.send({ results: result });
  });