所以我在集合中有一个唯一的数组,它的名称是"posts"。我试图更新该数组中的一个文档,并返回更新后的文档。我尝试了以下操作:
Posts.updateOne(
{},
{
$set : {
'posts.$[id].image' : cloudinaryUrl,
'posts.$[id].post' : req.body.updatedPost
}
},
{
arrayFilters: [
{ 'id._id': new ObjectId(req.body.postID) },
],
},
);
我得到了这个:
{
"data": {
"acknowledged": true,
"modifiedCount": 1,
"upsertedId": null,
"upsertedCount": 0,
"matchedCount": 1
}
}
还有这个
Posts.findOneAndUpdate(
{
'posts': {
$elemMatch: {
creatorId: req.userData.userId,
_id: new ObjectId(req.body.postID),
}
}
},
{
$set : {
'posts.$.image' : cloudinaryUrl,
'posts.$.post' : req.body.updatedPost
}
},
)
我得到了整个集合(我只是给你看1后):
{
"data": {
"_id": "63ddd8059b4324f25f69469e",
"__v": 0,
"posts": [
{
"post": "h123jjkkl",
"image": "",
"comments": [],
"likes": 0,
"creatorId": "63cdb85f5f2fb46f75781f7e",
"date": "2023-02-04T04:36:31.982Z",
"_id": "63dde0cf749dde1d574c29cf"
},
]
但是我无法获得更新的文档。有人能帮我吗?
1条答案
按热度按时间wljmcqd81#