node.js - Mongoose - delete subdocument array item -



node.js - Mongoose - delete subdocument array item -

i have little schema users:

{ username: string, contacts: array }

so illustration user's contacts this

{ username: "user", contacts: [{'id': objectid('525.....etc'), 'approved': false}, {'id':objectid('534.....etc'), 'approved': true}] }

now need delete item contacts do:

model.findbyidandupdate(23, {'$pull': { 'contacts':{'id':'525.....etc'} }});

but seems not working, no errors doesn't gets deleted, homecoming document user:

{ username: "user", contacts: [{'id':objectid('534.....etc'), 'approved': false}] }

how accomplish this?

the $pull operator performs conditions on array element on operating. seems question might not show working objectid value mongoose creates default array fields.

so query this, after importing objectid creation method:

model.findbyidandupdate(23, { '$pull': { 'contacts':{ '_id': new objectid(somestringvalue) } } });

or in fact can define "schema" little better, , mongoose "autocast" objectid based on "type" defined in schema:

var contactschema = new schema({ approved: boolean }); var userschema = new schema({ username: string, contacts: [contactschema] });

this allows mongoose "follow rules" strictly typed field definitions. knows have _id field each element of contacts array, , "type" of field objectid automatically re-cast "string" values supplied true objectid.

node.js mongodb mongoose mongodb-query

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -