簡體   English   中英

Mongoose,更新了嵌套數組

[英]Mongoose, updated nested array

我的問題是:如何在嵌套的 arrays 中查詢? 我想更改嵌套在數組“usersWhoLiked”中的 object 中的鍵“likeUp”中的值。 “usersWhoLiked”嵌套在數組“comments”中我如何使用 mongoose 做到這一點?

我在下面寫的請求......不起作用,但與 StackOverflow 帖子中給出的答案非常相似: ZCCADCDEDB567ABAE643E15DCF0974E503Z update update update nested object inside an array

這是我對帶有 updateOne 的 db 的請求:

try {
    const response = await Comments.updateOne(
        {
            productId,
            comments: { $elemMatch: { usersWhoLiked: { $elemMatch: { userId } } } },
        },
        {
            $set: { 'comments.$[outer].usersWhoLiked.$[inner].likeUp': likes.up },
        },
        {
            arrayFilters: [{ 'outer._id': commentId }, { 'inner._userId': userId }],
        }
    ).exec();
    return res.status(201).json({ response });
} catch (err) {
    console.log(err);
    return res.send(err);
}

這是我正在嘗試更新的集合:

    {
  "_id": {
    "$oid": "6307569d2308b78b378cc802"
  },
  "productId": "629da4b6634d5d11a859d729",
  "comments": [
    {
      "userId": "62f29c2c324f4778dff443f6",
      "userName": "User",
      "date": "2022.08.25",
      "confirmed": true,
      "likes": {
        "up": 0,
        "down": 0
      },
      "content": {
        "rating": 5,
        "description": "Nowy komentarz"
      },
      "_id": {
        "$oid": "630756b22308b78b378cc809"
      },
      "usersWhoLiked": [
        {
          "userId": "62f29c2c324f4778dff443f1",
          "likeUp": true,
          "_id": {
            "$oid": "6307572d2308b78b378cc80e"
          }
        },
        {
          "userId": "62f29c2c324f4778dff443f2",
          "likeUp": true,
          "_id": {
            "$oid": "6307572d2308b78b378cc80c"
          }
        }
      ]
    }
  ],
  "__v": 0
}

用於評論收集的 Mongooes 架構:

const commentSchema = new Schema({
    productId: String,
    comments: [
        {
            userId: String,
            userName: String,
            date: String,
            confirmed: Boolean,
            likes: {
                up: {
                    type: Number,
                    default: 0,
                },
                down: {
                    type: Number,
                    default: 0,
                },
            },
            content: {
                rating: Number,
                description: String,
            },
            usersWhoLiked: [{ userId: String, likeUp: Boolean }],
        },
    ],
});

我想問題出在您的arrayFilters運算符上,因為您試圖按不存在的字段_userId進行過濾:

arrayFilters: [{ 'outer._id': commentId }, { 'inner._userId': userId }],

我設法使用以下查詢更新了likeUp值:

db.collection.update({
  _id: ObjectId("6307569d2308b78b378cc802")
},
{
  $set: {
    "comments.$[user].usersWhoLiked.$[like].likeUp": false
  }
},
{
  arrayFilters: [
    {
      "user._id": ObjectId("630756b22308b78b378cc809")
    },
    {
      "like.userId": "62f29c2c324f4778dff443f1"
    }
  ]
})

在 MongoDB 操場上試一試: https://mongoplayground.net/p/XhQMNBgEdhp

暫無
暫無

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

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