How to remove Object from array using mongoose
<p>I'm trying to remove an object from an array in a document using mongoose.</p>

<p>The Schema is the following:</p>

<pre><code>var diveSchema = new Schema({
//irrelevant fields
    divers: [{
        user: { type: Schema.Types.ObjectId, ref: 'User', required: true },
        meetingLocation: { type: String, enum: ['carpool', 'onSite'], required: true },
        dives: Number,
        exercise: { type: Schema.Types.ObjectId, ref: 'Exercise' },
    }]
});
</code></pre>

<p>a possible entry can be</p>

<pre><code>{
    //irrelevant fields
    "divers": [
        {
            "_id": "012345678",
            "user": "123456789",
            "meetingLocation": "carpool",
            "exercise": "34567890",
        },
        {
            "_id": "012345679",
            "user": "123456780",
            "meetingLocation": "onSite",
            "exercise": "34567890",
        }
    ]
}
</code></pre>

<p>Say I want to remove the entry where <code>user</code> is <code>123456789</code> (note I do not know the <code>_id</code> at this point).</p>

<p>How do I do this correctly?</p>

<p>I tried the following:</p>

<pre><code>        var diveId = "myDiveId";
        var userIdToRemove = "123456789"
        Dive.findOne({ _id: diveId }).then(function(dive) {
            dive.divers.pull({ user: userIdToRemove });
            dive.save().then(function(dive) {
                //do something smart
            });
        });
</code></pre>

<p>This yieled no change in the document.</p>

<p>I also tried </p>

<pre><code>Dive.update({ _id: diveId }, { "$pull": { "divers": { "diver._id": new ObjectId(userIdToRemove) } } }, { safe: true }, function(err, obj) {
    //do something smart
});
</code></pre>

<p>With this I got as result that the entire <code>divers</code> array was emptied for the given dive.</p>