Mongoose / mongodb - Remove embedded document

967 views
Skip to first unread message

Alex Brown

unread,
Apr 20, 2012, 8:54:55 AM4/20/12
to mongoo...@googlegroups.com

 have a Schema (called Event) with data that looks like this:

{ "_id" : ObjectId( "4f8dcb06ee21783d7400003c" ),
  "venue" : ObjectId( "4f8dcb06ee21783d7400003b" ),
  "name" : "Some event",
  "webite: "www.whatever.com",
  "attendees" : [
                    { "_id" : ObjectId( "4f8dfb06ee21783d7134503a" ), "firstName" : "Joe", "lastName" : "Blogs", "emailAddress" : "so...@thing1.com" },
                    { "_id" : ObjectId( "4f8dfb06ee21783d7134503b" ), "firstName" : "John", "lastName" : "West", "emailAddress" : "so...@thing2.com" }
                    { "_id" : ObjectId( "4f8dfb06ee21783d7134503c" ), "firstName" : "Simon", "lastName" : "Green", "emailAddress" : "so...@thing3.com" }
                    { "_id" : ObjectId( "4f8dfb06ee21783d7134503d" ), "firstName" : "Harry", "lastName" : "Smith", "emailAddress" : "so...@thing4.com" }
                ],
  "eventType" : "Party"
}

How can I 'delete' the attendees object with id 4f8dfb06ee21783d7134503c?

Basically something like....

Event.findOne('attendees._id' : ObjectId('4f8dfb06ee21783d7134503c'), function(err, eventItem){
    //delete the attendee embedded doc with id 4f8dfb06ee21783d7134503c
});

James Carr

unread,
Apr 20, 2012, 10:05:08 AM4/20/12
to mongoo...@googlegroups.com

You could use popylate() and then just delete.

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en

Alex Brown

unread,
Apr 20, 2012, 10:47:42 AM4/20/12
to mongoo...@googlegroups.com
even easier than that...

i did:


Event.findOne('attendees._id' : ObjectId('4f8dfb06ee21783d7134503c'), function(err, eventItem){
    eventItem.id('id_goes_here').remove() // this works....
});

see bottom of:
Reply all
Reply to author
Forward
0 new messages