如何在猫鼬和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"
任何帮助将不胜感激,谢谢!