如何从猫鼬的嵌套数组中拉出对象

时间:2019-06-05 20:19:26

标签: javascript mongoose nestjs

我无法删除集合中的对象表单数组。我正在使用猫鼬和Nest.js

我正在尝试从数组beers中删除对象(啤酒)。

我也尝试过findOneAndUpdatethis.userModel.update({user:user}, {$pull:{'beers.id':id});

这是我的收藏

[
    {
        "beers": [
            {
                "id": 4,
                "name": "Pilsen Lager",
                "tagline": "Unleash the Yeast Series.",
                "first_brewed": "09/2013",
                "description": "Our Unleash the Yeast series was an epic experiment into the differences in aroma and flavour provided by switching up your yeast. We brewed up a wort with a light caramel note and some toasty biscuit flavour, and hopped it with Amarillo and Centennial for a citrusy bitterness. Everything else is down to the yeast. Pilsner yeast ferments with no fruity esters or spicy phenols, although it can add a hint of butterscotch.",
                "image_url": "https://images.punkapi.com/v2/4.png",
                "abv": 6.3,
                "ibu": 55,
                "target_fg": 1012,
                "target_og": 1060,
                "ebc": 30,
                "srm": 15,
                "ph": 4.4,
                "attenuation_level": 80,
                "volume": {
                    "value": 20,
                    "unit": "litres"
                },
                "boil_volume": {
                    "value": 25,
                    "unit": "litres"
                },
                "method": {
                    "mash_temp": [
                        {
                            "temp": {
                                "value": 65,
                                "unit": "celsius"
                            },
                            "duration": null
                        }
                    ],
                    "fermentation": {
                        "temp": {
                            "value": 9,
                            "unit": "celsius"
                        }
                    },
                    "twist": null
                },
                "ingredients": {
                    "malt": [
                        {
                            "name": "Extra Pale",
                            "amount": {
                                "value": 4.58,
                                "unit": "kilograms"
                            }
                        },
                        {
                            "name": "Caramalt",
                            "amount": {
                                "value": 0.25,
                                "unit": "kilograms"
                            }
                        },
                        {
                            "name": "Dark Crystal",
                            "amount": {
                                "value": 0.06,
                                "unit": "kilograms"
                            }
                        },
                        {
                            "name": "Munich",
                            "amount": {
                                "value": 0.25,
                                "unit": "kilograms"
                            }
                        }
                    ],
                    "hops": [
                        {
                            "name": "Centennial",
                            "amount": {
                                "value": 5,
                                "unit": "grams"
                            },
                            "add": "start",
                            "attribute": "bitter"
                        },
                        {
                            "name": "Amarillo",
                            "amount": {
                                "value": 5,
                                "unit": "grams"
                            },
                            "add": "start",
                            "attribute": "bitter"
                        },
                        {
                            "name": "Centennial",
                            "amount": {
                                "value": 10,
                                "unit": "grams"
                            },
                            "add": "middle",
                            "attribute": "flavour"
                        },
                        {
                            "name": "Amarillo",
                            "amount": {
                                "value": 10,
                                "unit": "grams"
                            },
                            "add": "middle",
                            "attribute": "flavour"
                        },
                        {
                            "name": "Centennial",
                            "amount": {
                                "value": 17.5,
                                "unit": "grams"
                            },
                            "add": "end",
                            "attribute": "flavour"
                        },
                        {
                            "name": "Amarillo",
                            "amount": {
                                "value": 17.5,
                                "unit": "grams"
                            },
                            "add": "end",
                            "attribute": "flavour"
                        }
                    ],
                    "yeast": "Wyeast 2007 - Pilsen Lager™"
                },
                "food_pairing": [
                    "Spicy crab cakes",
                    "Spicy cucumber and carrot Thai salad",
                    "Sweet filled dumplings"
                ],
                "brewers_tips": "Play around with the fermentation temperature to get the best flavour profile from the individual yeasts.",
                "contributed_by": "Ali Skinner <AliSkinner>"
            }
        ],
        "_id": "5cf805352275c02e985b8780",
        "user": "729MhPMOPkbPW2j2lNgil0Xl25l1",
        "__v": 0
    }
]

这是我删除对象表格数组的功能

  async removeBeerFromUser(user, id) {
    this.userModel.update({user:user}, {$pull:{beers:{id:id}}, (err, val) => {
      console.log(val);
    });
  }

但无能为力

没有错误。控制台日志返回空啤酒阵列。但是当我从邮递员那里检查它时,它并不为空

0 个答案:

没有答案