如何更新模式对象的一部分而不替换它(node.js api 中的 Mongoose 模式)?

发布于 2025-01-10 12:10:45 字数 1638 浏览 0 评论 0原文

我对 node.js 和 mongoose 非常陌生,所以如果我的问题没有使用正确的术语,我就直接告诉我。我很惊讶我这么快就走到这一步了!

我的主要架构是一个杂货清单,它可以有一个 listItemDict 对象和一个销售对象数组。我可以放置主模式中的任何字段,它只会更新该字段 - 其他字段保持原样。但是,如果我放置销售模式中的任何字段,它会替换那里的任何销售数据。

我的架构是这样定义的

var listItemDictSchema = mongoose.Schema(
    {
      name: String,
      details: String,
      aisle: String,
      aisleSort: Number
    }
  ); 

  var saleSchema = mongoose.Schema(
    {
      store: String,
      price: Number,
      quantity: Number,
      quantityUnit: String,
      details: String,
      startDate: Date,
      endDate: Date
    }
  )
  
  var schema = mongoose.Schema(
      {
        name: String,
        details: String,
        quantity: Number,
        quantityUnit: String,
        priority: String,
        forDate: Date
        listItemDict: listItemDictSchema,
        sales: [saleSchema]
      },
      { timestamps: true }
    );

这是一个示例 mongodb 条目

_id:621ca04410e73258802a619d
name:"Bread"
details:""
quantity:1
quantityUnit:"loaf"
priority:"Normal"
listItemDict:Object
sales:
Array:
0:Object
  _id:621ca66f10e73258802a61a5
  store:"Store 1"
  details:"BOGO"
  price:1.99
1:
  Object
  _id:621ca66f10e73258802a61a6
  store:"Store 2"
  price:2.2
  createdAt:2022-02-28T10:13:24.312+00:00
  updatedAt:2022-02-28T10:39:43.768+00:00

如果我 put

{
"quantity": 2
}

只是更新数量,其他一切都保持不变,我认为这是正确的行为。

如果我输入

"sales": [{
    "id": "621ca66f10e73258802a61a5",
    "store": "Store 3"
}]

它将用此替换所有销售信息。

如何在不替换所有销售数据的情况下更新一件销售对象?

I am very new to node.js and mongoose so bare with me if my question isn't using the right terms. I'm amazed I've gotten this far so quickly!

My main schema is a grocery list, which can have a listItemDict object, and an array of sale objects. I can PUT any field from the main schema and it will update just that field - the other fields stay as they were. But if I PUT any field from the sale schema, it replaces any sale data that was there.

My schema's are defined like this

var listItemDictSchema = mongoose.Schema(
    {
      name: String,
      details: String,
      aisle: String,
      aisleSort: Number
    }
  ); 

  var saleSchema = mongoose.Schema(
    {
      store: String,
      price: Number,
      quantity: Number,
      quantityUnit: String,
      details: String,
      startDate: Date,
      endDate: Date
    }
  )
  
  var schema = mongoose.Schema(
      {
        name: String,
        details: String,
        quantity: Number,
        quantityUnit: String,
        priority: String,
        forDate: Date
        listItemDict: listItemDictSchema,
        sales: [saleSchema]
      },
      { timestamps: true }
    );

This is a sample mongodb entry

_id:621ca04410e73258802a619d
name:"Bread"
details:""
quantity:1
quantityUnit:"loaf"
priority:"Normal"
listItemDict:Object
sales:
Array:
0:Object
  _id:621ca66f10e73258802a61a5
  store:"Store 1"
  details:"BOGO"
  price:1.99
1:
  Object
  _id:621ca66f10e73258802a61a6
  store:"Store 2"
  price:2.2
  createdAt:2022-02-28T10:13:24.312+00:00
  updatedAt:2022-02-28T10:39:43.768+00:00

If I put

{
"quantity": 2
}

just the quantity is updated and everything else stays the same, which I think is the right behaviour.

If I put

"sales": [{
    "id": "621ca66f10e73258802a61a5",
    "store": "Store 3"
}]

it replaces all sale info with this.

How can I update one piece of a sale object without replacing all sale data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛溺水了 2025-01-17 12:10:45

如果你想更新具体销售的数量,那么首先通过 id 选择销售文档,然后更新 $set 中你想要的字段:

SalesModel.findByIdAndUpdate('id',{
   $set:{
      quantity:10
   }
})

If you want to update the quantity of specific sales then pick sale document first by id and then update the fields you want in $set:

SalesModel.findByIdAndUpdate('id',{
   $set:{
      quantity:10
   }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文