Remove embedded document

49 views
Skip to first unread message

Nguyen Nhan

unread,
Apr 23, 2013, 5:34:02 AM4/23/13
to yiimong...@googlegroups.com
Hi all,

I've document with embedded:

{
     name: "ABC",
     description: "Blah Blah",
     like: [
            {
                  id:2,
                  user: ObjectId(""),
                  date: 1231450313
            },
            {
                  id:3,
                  user: ObjectId(""),
                  date: 1231450313
            }  
     ]
}

How do I remove one embedded document by id, or by index like[0], like[1]?
Thanks.

Uveg Pohar

unread,
Apr 23, 2013, 5:47:46 AM4/23/13
to yiimong...@googlegroups.com, Nguyen Nhan
hi

in mongodb, you can use $pull
http://docs.mongodb.org/manual/reference/operator/pull/

example:
> db.testtable.findOne({_id: ObjectId("5176572e518872d3af17f8ba")});
{
        "_id" : ObjectId("5176572e518872d3af17f8ba"),
        "a" : [
                {
                        "id" : 1,
                        "name" : "egy"
                },
                {
                        "id" : 2,
                        "name" : "ketto"
                },
                {
                        "id" : 3,
                        "name" : "harom"
                }
        ]
}
> db.testtable.update({_id: ObjectId("5176572e518872d3af17f8ba")}, {$pull: {a: {id: 2}}});
> db.testtable.findOne({_id: ObjectId("5176572e518872d3af17f8ba")});
{
        "_id" : ObjectId("5176572e518872d3af17f8ba"),
        "a" : [
                {
                        "id" : 1,
                        "name" : "egy"
                },
                {
                        "id" : 3,
                        "name" : "harom"
                }
        ]
}

as i remember, in yiimongodbsuite you can use updateAll for this
http://canni.github.io/YiiMongoDbSuite/xhtml/advanced.partial-batch-update.html

fec
--
You received this message because you are subscribed to the Google Groups "YiiMongoDbSuite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yiimongodbsiu...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Nguyen Nhan

unread,
Apr 23, 2013, 6:16:41 AM4/23/13
to yiimong...@googlegroups.com
Thank poHar,

But i want to remove it, don't want update with  new value.
How YMDS remove embedded document?

poHar uVeg

unread,
Apr 23, 2013, 6:22:59 AM4/23/13
to yiimong...@googlegroups.com
it will remove the {id:2, ...} object, from the list
if you see my example, the first findOne result in the array "a" has 3 embedded document {id: 1, ...}, {id: 2, ...}, {id: 3, ...}
the collection.update(filter, {$pull: {a: {id: 2}}}) will remove the {id:2, ...} object from the array "a"
as you can see the findOne result after the update


2013/4/23 Nguyen Nhan <nxt...@gmail.com>
--
Reply all
Reply to author
Forward
0 new messages