简体   繁体   中英

pull subdocument in array mongodb

i have in mongodb this structure

{doc:
    { array_doc:[....//many documents]}
}

im using mongoskin in mongodb 2.2 with nodejs 0.8

var code_doc='HSKD41814541211';
var db = mongo.db(perm+"@127.0.0.1:27017/database",{safe:true});
    db.collection('comercio').findAndModify(
    {
        'code':code,
        'email':email,
    },[],{$pull:{'doc.array_doc':{'doc.array_doc.code_doc':code_doc}}},{new:true},function(err,result)

i want to erase a document from array_doc array that have many documents inside, each document inside the array have a unique code_doc... i want to erase the document that match with the variable code_doc='HSKD41814541211';

tnx

Don't replicate your key in the value being pulled. Your $pull operator should look like this instead:

{$pull: {'doc.array_doc': {code_doc: code_doc}}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM