【问题标题】:How can a find() access a document field for an operation? (MongoDB, nodeJS)find() 如何访问文档字段以进行操作? (MongoDB,nodeJS)
【发布时间】:2021-12-03 08:49:01
【问题描述】:

你好我想知道daily_price > = price_clic * number_view

Model.find({

 daily_price: { 

  $gte: price_clic * number_view

 }

})

const schema = mongoose.Schema({

  price_clic: { type: Number, required: true },
  number_view: { type: Number, required: true, default: 0 },
  daily_price: { type: Number, default: 0 },

})

非常感谢

【问题讨论】:

  • 在计算中使用文档字段时,您需要使用 $expr 运算符将聚合运算符 $gte$multiply 括起来。

标签: node.js mongodb mongodb-query


【解决方案1】:

以下代码解决了我的问题:

$expr: {

  $gte: [
    "$daily_price", { $multiply: ["$price_clic", "$number_view"] },
  ],
},

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 2017-04-21
    • 1970-01-01
    • 2023-01-12
    • 2017-08-14
    相关资源
    最近更新 更多