0

Suppose the mongodb document(table) 'users' is

{
    "_id": "6065923bc5d509071857f6e6",
    "version": [
        {
            "name": "1.1.0",
            "status": "not Approved",
            "_id": "606592b1c5d509071857f6e7",
            "files": [
                {
                    "_id": "606592b3c5d509071857f6e8",
                    "url": "https://google.com/1.png",
                    "status": "awaiting"
                },
                {
                    "_id": "606592b8c5d509071857f6e9",
                    "url": "https://google.com/2.png",
                    "status": "awaiting"
                }
            ]
        }
    ]
}

I want to update the status of this "***https://google.com/1.png***" image Can we do using findOneAndUpdate? because i want to return the updated Object

2 Answers 2

2

Let's say you want to search for the awaiting status for the collection users

db.users.find({"version.files.status":"awaiting"}).pretty()

It will automatically search within the arrays.

Updating, however, is a bit different and you should use arrayFilter:

db.users.updateOne(
  {
    "version.files._id": "606592b3c5d509071857f6e8",
  },
  {
    $set: {
      "version.$[version].files.$[file].url": "something else",
    },
  },
  {
    arrayFilters: [
      { "version._id": "606592b1c5d509071857f6e7" },
      { "file._id": "606592b3c5d509071857f6e8" },
    ],
  }
);

Make sure to have an index on your find query:

For the above example

db.users.createIndex({"version.files._id":1},{background:true})

Many more example available on the Mongo Doc


EDIT

As per your comment, here is a working example:

db.users.findOneAndUpdate(
  { "version.files._id": "606592b3c5d509071857f6e8" },
  {
    $set: {
      "version.$[version].files.$[file].url": "Test URL",
      "version.$[version].files.$[file].status": "Test status",
    },
  },
  {
    returnNewDocument: true,
    arrayFilters: [
      { "version._id": "606592b1c5d509071857f6e7" },
      { "file._id": "606592b3c5d509071857f6e8" },
    ],
  }
);

enter image description here

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

6 Comments

can we do using findOneAndUpdate() ??
Of course, just replace the updateOne by findOneAndUpdate and use the returnNewDocument: true boolean to return the updated doc (alongside arrayFilters) docs.mongodb.com/manual/reference/method/…
Yes working.. but when i update "status" of file in that case "STATUS" is not updating I am using this { $set: { "version.$[version].files.$[file].status": data.status, "version.$[version].files.$[file].url": data.url } },
Yes, you need to provide all the fields you want to update in the $set object
You need to add returnNewDocument:true as well
|
1

You can do it like this:

    await db.collection('users').updateOne(
        { _id: users._id },
        { $set: { "version.$[version].files.$[file].url": "newStatus" } },
        {
            arrayFilters: [
                { "version._id": "606592b1c5d509071857f6e7" },
                { "url": "https://google.com/1.png" }
            ]
        }
    );

Edit:

If there's more than one record with "https://google.com/1.png" It'll update the status of the first record it encountered.

Edit2: Thanks callmemath for fix my mistake

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.