findAndModify and Pull operation.

314 views
Skip to first unread message

zhengchun

unread,
Jul 2, 2012, 11:53:40 AM7/2/12
to mongod...@googlegroups.com
hello,everyone,i have similar following data 
{
  _id,
 ....
 items:[
    {
       id:1,
       ....
    },
    {
      id:2
      ....
    }
 ]
}

now,i want to delete the items.id=1 and return the other remain items .
my code like this:
            var query = Query.EQ("items.id", 1);
            var update = Update.Pull("items", Query.EQ("id",1"));
            var fields = Fields.Include(new string[] { "_id", "items" });
           var result=this.Collection.FindAndModify(query, SortBy.Null, update, fields, true, false);

when i runing the above code,i get the error.

Command 'findAndModify' failed: E11000 duplicate key error index: tobobay_prods.products.$items.id_1  dup key: { : null } (response: { "lastErrorObject" : { "err" : "E11000 duplicate key error index: tobobay_prods.products.$items.id_1  dup key: { : null }", "code" : 11001, "n" : 0, "connectionId" : 52, "ok" : 1.0 }, "

someone can give me some suggestion?how Pull some item and return entire data?
thanks.

zhengchun

unread,
Jul 2, 2012, 2:48:41 PM7/2/12
to mongod...@googlegroups.com
sorry,i notice this error is the items.id as index because it.if i delete items.id index,everything is good.

http://stackoverflow.com/questions/8639212/e11000-duplicate-key-error-index-mongodb-unusual-error 

在 2012年7月2日星期一UTC+8下午11时53分40秒,zhengchun写道:

Dwight Merriman

unread,
Jul 2, 2012, 11:45:36 PM7/2/12
to mongod...@googlegroups.com
$pull on "items.id":1 won't work as "items.id" is not an array.

Dwight Merriman

unread,
Jul 3, 2012, 10:11:05 AM7/3/12
to mongod...@googlegroups.com
whoops, pull on "items.id" doesn't work but {items:{id:...}} does work in newer versions.

db.mycollection.update({"items.id":1},{$pull:{items:{id:1}}}) // ok
Reply all
Reply to author
Forward
0 new messages