0

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

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

I can not give feedback in answer score (too low reputation...), but thank you from the bottom of my hearth.... I really struggled for 2 days to working that out... and it turned out... that it was just typo. Thank you <3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.