简体   繁体   English

通过指示值从MongoDB文档中提取1级深度数组元素

[英]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", ? 我如何从下面的MongoDB文档中拉出groups数组中所有值为"abreiseValue":"25.02.2018",数组元素"abreiseValue":"25.02.2018",以便在groups数组中不再有值为"abreiseValue":"25.02.2018",数组元素"abreiseValue":"25.02.2018", I want to do it in NodeJS. 我想在NodeJS中做到这一点。

(In my real document I have more embedded documents in the groups array with the same structure) (在我的真实文档中,在具有相同结构的groups数组中有更多嵌入式文档)

 { "_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 ) (基于以下链接: 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. 有关此方法的详细信息,请参见$ pull运算符findAndModify()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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