在更新查询中使用$ expr

时间:2018-08-03 07:37:43

标签: javascript node.js mongodb mongoose mongodb-query

如何在猫鼬和MongoDB查询中使用$expr

当我尝试使用findAndModify

const createConversation = await Conversations.findAndModify({
  query: {
    type: 'P', members: { $in: payload.members },
    $expr: { $gt: [ { $size: "$members" }, 1 ] }
  },
  update: { $set: payload },
  upsert: true, new: true, runValidators: true, setDefaultsOnInsert: true
})

上面的错误给我带来了明显的错误,因为它是MongoDB函数,而不是猫鼬

TypeError: _conversationsModel2.default.findAndModify is not a function

以及当我尝试使用findOneAndUpdate

const createConversation = await Conversations.findOneAndUpdate(
  {
    type: 'P', members: { $in: payload.members },
    $expr: { $gt: [ { $size: "$members" }, 1 ] }
  },
  { $set: payload },
  { upsert: true, new: true, runValidators: true, setDefaultsOnInsert: true, strict: false }
)

它给了我

"name": "MongoError",
"message": "$expr is not allowed in the query predicate for an upsert"

任何帮助将不胜感激,谢谢!

0 个答案:

没有答案