Home > Enterprise >  Mongoose, updated nested array
Mongoose, updated nested array

Time:08-29

My question is: How can I query in the nested arrays? I want to change value in key "likeUp" which is nested inside object in array "usersWhoLiked". Where "usersWhoLiked" is nested in array "comments" How Can I do that with mongoose ?

Request that I wrote beneath... do not work, but is very similar to answer given in StackOverflow post: Mongoose update update nested object inside an array

This is my request to db with updateOne:

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);
}

This is the collection, that I am trying to update:

    {
  "_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 schema for comment collection:

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 }],
        },
    ],
});

CodePudding user response:

I guess the problem is with your arrayFilters operator, because you are trying to filter by field _userId which does not exist:

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

I managed to update the likeUp value using the following query:

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

Try it on MongoDB playground: https://mongoplayground.net/p/XhQMNBgEdhp

  • Related