1

i have a mongoose schema like this

id: { type: String, required: true, unique: true, default: uniqueUUID },
    description: { type: String },
    period: {
        id: { type: String, default: uuid.v1 },
        start: { type: Date, default: Date.now },
        due: { type: Date },
        dueWarnByHours: { type: Number, integer: true },
        newnessByHours: { type: Number, integer: true },
    },

I want to remove all the elements under period embedded schema that matches the period.id.

i'm currently trying it like

 WorkItem.remove({ 'id': workItemId, 'period.id': periodId }, function(err, callID) {
        if (err) return console.error(err);
        console.dir(callID + "Successfully removed the workItem from the database");
    });

it's not working and it's not an array

Community
  • 1
  • 1
Kokulan
  • 1,316
  • 3
  • 19
  • 35

1 Answers1

1

Remove the embedded document using $unset as follows:

WorkItem.update(
    { "id": workItemId, "period.id": periodId }, 
    { "$unset": { "period": "" } },
    function(err, callID) {
        if (err) return console.error(err);
        console.dir(callID + "Successfully removed the period from the database");
    }
);
chridam
  • 100,957
  • 23
  • 236
  • 235