簡體   English   中英

如何在 mongodb 中更新 object 的嵌套數組

[英]how to update nested array of object in mongodb

這是我在 mongoDB 中的數據庫集合。我正在使用 mongoose 更新評論數組中的數據

{
        "author": {
            "email": "user@example.com",
            "userName": "John"
        },
        "_id": "63bc20741475b40323d6259f",
        "title": "this is blog",
        "description": "description",
        "blogBanner": "https://github.com//routers/userRouter.js",
        "views": "0",
        "comments": [
            {
                "userId": "63b919840ae5303938fb1c17",
                "comment": "first comment",
                "_id": "_id:63bbdbdb7018eabb752c0e58
            },
            {
                "userId": "63b919840ae5303938fb1c17",
                "comment": "second comment",
                "_id": "63bbdbdb7018eabb752ce55"
            }
        ],
        "reaction": [
            {
                "userEmail": "gias@gmail.com",
                "react": "love"
            }
        ],
        "date": "2023-01-09T14:09:32.810Z",
        "__v": 0
    }

在這里,我想使用comment._id: 63bbdbdb7018eabb752c0e58

如何使用comment._id更新單個評論;

用例:當用戶想要使用comment._id更新他/她的評論時

在這里,我想使用comment._id: 63bbdbdb7018eabb752c0e58

如何使用comment._id更新單個評論;

用例:當用戶想要使用comment._id更新他/她的評論時

請閱讀以下文檔: https://www.mongodb.com/docs/manual/reference/operator/update/positional/#update-documents-in-an-array

Model.update(
 {  
 '_id': '63bc20741475b40323d6259f',
 'comments._id':'63bbdbdb7018eabb752ce55' 
},
  { $set: { "comments.$.comment" : 'Edited Text' } }
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM