在mongoDB聚合中的函数中引用`this`

时间:2017-08-11 09:40:23

标签: node.js mongodb mongoose aggregation-framework

我尝试使用

查询集合
function activeNow(){
   //some logic
   //return a number between -1 and 7
}

conn.menu.aggregate([
  { 
    $project: { 
      descrizione: 1,
      validity: () => activeNow(this) //refer to current menu
    }
  },
  {
    $sort: {
      validity: 1
    }
  }
]).exec().then(menu => {
  // menu ordered by activeNow result
})

实际上它只返回字段_id和describezione,如:

[ { _id: 59898b6a41e78f02e4206034, descrizione: [ [Object] ] },
  { _id: 598c312bc0035d313c26c68b, descrizione: [ [Object] ] } ]

将Node与Mongoose和MongoDB 3.2.8一起使用

0 个答案:

没有答案