是否可以使用mongoDB和Node.js返回多个子文档?

时间:2017-03-28 15:22:11

标签: node.js mongodb mongoose subdocument embedded-documents

我目前有一个架构,目前看起来像:

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开发人员”#39;在第二个用户中输入,但我似乎无法返回任何后续条目,我只是想知道它是一个MongoDB问题,我只能返回一个匹配每个用户的子文档?或者我的代码中有什么问题吗?

返回的对象可以在下面看到,结果对象返回两个用户,每个用户有一个位置对象,但是应该有3个。 enter image description here

假设我在位置数组中有一个数组,我该如何访问它?

我目前的代码:

  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
            }],
  }],

1 个答案:

答案 0 :(得分:6)

你可以使用$ unwind来获取它,它会从集合中获取多个条目,这与元素匹配或'不同。'只返回第一个匹配数据的运算符

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