在MongoDB中搜索并推送数组到嵌套对象数组

时间:2018-02-28 09:06:31

标签: node.js mongodb nosql

  "date_added": {
    "$date": "2018-02-27T21:34:31.144Z"
},
"malls": [
    {
        "name": "DFM",
        "geocoordinates": "-6.7726935,39.2196418",
        "region": "Kentucky",
        "show_times": [],
        "_id": {
            "$oid": "5a95d3ed053cc1444eadaeae"
        }
    },
    {
        "name": "MkHouse",
        "geocoordinates": "-6.8295944,39.2738459",
        "region": "Kenon",
        "show_times": [],
        "_id": {
            "$oid": "5a95d429053cc1444eadaeaf"
        }
    }
],
"title": "Black Panther",

我需要查找/查询名称为==“DFM”的商城并将数据推送到show_times数组,任何人都可以帮忙!哪种方法可以解决这个问题。我已经使用_id查询了它并且有了这个文档。现在我怎么能推送show_times?我正在使用mongoose v5.5.1

1 个答案:

答案 0 :(得分:1)

试试这个,它基本上插入showtime,它在商场数组中找到名称字段等于DFM,$运算符用于此

model.update(
       { _id: "givenObjectId",
         "malls.name" : "DFM"
       },
       {
          $push : {"malls.$.show_times" : data }
       }
    )

More details on the $ operator