MongoDB
 sql >> Datenbank >  >> NoSQL >> MongoDB

Mongoose aktualisiert tiefe Arrays

Am Ende nur das Array durchlaufen:

Schemas.Client.findOne({ _id: id, 'customers._id': customerID }).select('customers.$.addresses').exec(function (err, client) {
  if (err) { return next(err); }
  if (!client) { return next(); }
  if (client.customers[0].addresses.length) {
    var addresses = client.customers[0].addresses;
    for (var i = 0; i < addresses.length; i++) {
      addresses[i].isDefault = false;
    }
    client.save(function (err) {
      if (err) { return next(err); }
      // finish
    });
  }
});