【问题标题】:How to change nested properties in MongoDB using Express?如何使用 Express 更改 MongoDB 中的嵌套属性?
【发布时间】:2020-01-10 14:29:26
【问题描述】:

我的 MongoDB 中有一个对象:

{
  "title": "Amazing Spider Man 2",
  "description":
    "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s",
  "image": "https://wallpaperaccess.com/full/35357.jpg",
  "price": 150,
  "screen": 1,
  "dates": [
    {
      "date": "2014-02-01T09:28:56.321-10:00",
      "rows": [
        {
          "seats": [
            { "booked": true },
            { "booked": true },
            { "booked": false }
          ]
        },
        {
          "seats": [
            { "booked": false },
            { "booked": true },
            { "booked": false}
          ]
        },
        {
          "seats": [
            { "booked": false },
            { "booked": true},
            { "booked": false }
          ]
        }
      ]
    },

我想使用 Express 更新“已预订”的属性。而且我不知道如何以最好的方式做到这一点:) 我对 Node.js 和 Mongo 很陌生,所以我会非常感谢任何帮助。 谢谢!

【问题讨论】:

  • 你在app里有express运行和配置吗?
  • 是的。我已经写了简单的获取、发布路线。
  • 你是如何通过 mongoose 在节点中访问 mongodb 的?
  • 是的,我正在使用猫鼬。

标签: javascript node.js mongodb express


【解决方案1】:

您可以使用the update array operator! 更新文档。

这是来自 mongodb shell 用0表示在数组中的位置

db.getCollection("your collection name").update(criteria, $set: {"dates.0.rows.0.seats.0.booked" : false})

您可以向https://docs.mongodb.com/manual/reference/operator/update-array/中的数组更新运算符学习

【讨论】:

  • 感谢您的回复!但是,如果我想接收例如信息怎么办。关于前端的索引?我该怎么做这样的事情:dates.${req.body.dateIdx}....
  • 如果是整数值就没有问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-03
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
  • 2019-12-14
  • 2021-02-25
相关资源
最近更新 更多