简体   繁体   中英

Pull 1 level deep array element from MongoDB document by indicating value

How can I pull from the following MongoDB document all array elements from the groups array with the value "abreiseValue":"25.02.2018", so that there are no more array elements in the groups array with the value "abreiseValue":"25.02.2018", ? I want to do it in NodeJS.

(In my real document I have more embedded documents in the groups array with the same structure)

 { "_id":{ "$oid":"5a48d7c2f36d284acc10a64d" }, "department":"edelweissKaminStube", "tables":[ { "arrayIndex":0, "department":"edelweissKaminStube", "number":"80", "topValue":"345", "leftValue":"250", "bgColor":"#b7b7b7", "isBesetzt":"true", "placeholder":"true", "border":"solid 3px #f3efe4", "width":"40", "height":"35", "groups":[ { "nameValue":"Prahst, Andreas", "zimmernummerValue":"705", "anreiseValue":"18.02.2018", "abreiseValue":"25.02.2018", "personenAnzahlValue":"2/0", "notiz2Value":"2*\€30 Spa+TG", "notiz1Value":"2 Erw. neu", "traceValue":"-", "bemerkungValue":"Sie: Gluten und Milchunverträglichkeit " } ] } ] } 

This is how I tried it, but it did not work. (Based on the following link: how-to-remove-array-element-in-mongodb )

 db.hubertusTables.update({department: "edelweissKaminStube", "tables.number": "80"}, {$pull: {"tables.$.groups": {abreiseValue: "25.02.2018"} } }, {multi: true}); 

Thanks for the help!

You can try the following query:

db.collectionName.findAndModify({
    query: {
        department: "edelweissKaminStube"
    },
    update: {
        $pull: { "tables.$[element].groups": { "abreiseValue": "25.02.2018" } }
    },
    arrayFilters: [ 
        { "element.number": "80" }
    ]
});

Details about this approach can be found on $pull operator and on findAndModify() method.

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