【发布时间】:2020-08-14 01:26:31
【问题描述】:
我在使用 $elemMatch 进行更新时遇到了一些问题。 当我尝试匹配或查找时,它会显示写入结果。
.updateOne(
{ _id: req.body.sid },
{ $pull: {comments: { $elemMatch: { fid: '5ea9c87d2d5c8f491cae8818' } }}}
).then((res)=>{
console.log(res)
}).catch((err)=>{
console.log(err)
})
结果:
{ n: 1, nModified: 0, ok: 1 }
这是使用 find 的查询结果:
.find({
$and: [
{ _id: req.body.sid },
{comments: { $elemMatch: { fid: '5ea9c87d2d5c8f491cae8818' } }}
]
})
查找的示例文档结果:
[
{
_id: 5ea88b12423e6e4b3ce01956,
comments: [{"fid":"5ea9c87d2d5c8f491cae8818","comment":"ok"}, {"fid":"5ea9c87d2d5c8f491cae8899","comment":"ok"}],
date: 2020-04-28T00:00:00.000Z,
title: 'Fotos para o Álbum de ensaios',
__v: 5
}
]
谁能帮帮我?谢谢!!
【问题讨论】:
-
你的
comments: [ [Array] ]看起来像一个对象数组吗? -
它是一个对象数组:[{"fid":"5ea9c87d2d5c8f491cae8818","comment":"ok"}]
标签: node.js mongodb mongoose mongodb-query