1

Update with mongoose is not working when there is a dot notation in one of the key names.

I am trying to update a document with mongoose which has the following structure:

{
  "test": {
    "directory": {
      "testFile.js": {
        "file": {
          "contents": "test"
        }
      }
    }
  }
}

As you can see it includes a key name: 'testfile.js', this is causing an issue when I try to update the content of the file:

const updatedFileTree = await this.fileTree
  .findByIdAndUpdate(
    { _id: id },
    {
      $set: {
        'test.directory.testFile.js.file.contents': 'new value',
      },
    },
  )
  .exec();
return updatedFileTree;

Because it is already using dot notations, is there a way to make this work because I have to keep the structure I have right now (with dots).

Arend
  • 11
  • 1
  • How important is it to you to use that schema? I'm guessing that you'll continue to have random friction/headaches like this when using the dot even if you solve this problem (which you could probably accomplish cus `$setField`). Relatedly, it's often not a great design to store data as field names (e.g. you can't index it and it may be difficult to query). Consider using a different schema with a `fileName` sibling field to `contents` – user20042973 Jun 14 '23 at 00:45
  • @user20042973 It is quite important to use this schema because it is a filesystem structure I need, if I decide to restructure it I will have to restructure it each time I read/update/create a new item. It is possible but I will have to loop over the nested object (which can get pretty big) and restructure it before I save the data and restructure it again when I return the data to the FE. I wanted to avoid this by updating part of the object, I understand it is not great design to store data as a field name, unfortunately I don't have any control over this filesystem structure. – Arend Jun 14 '23 at 08:59

0 Answers0