1

I asked the following the question the other day: MongoDB $push to nested child - recursive data structure

Basically, I wanted to edit/add data to recursive child in MongoDB.

To solve this issue, I created a function in my backend like the following:

  1. Go through recursive data stored in MongoDB and find child id
  2. edit/add data in backend
  3. $set to update the entire data in MongoDB

My question is, would 2 users updating the same data at the same time cause any issues? Since I am using $set instead of $push etc.

  async update(cname: string, id: string, elementIndex: number, elements: Element[]) {
    const db = await this.getDB();
    const collection = db.collection(cname);
    const key = 'elements.' + String(elementIndex);
    return new Promise((resolve, reject) => {
      collection.updateOne({ id: id }, { $set: { [key]: elements } }, (err, res) => {
        if (err) {
          reject(err);
        }
        resolve(res);
      });
    });
  }

0 Answers0