简体   繁体   English

如何仅更新 Mongodb 中现有对象的某些键?

[英]How do I update only certain keys of existing objects in Mongodb?

So this is my case:所以这是我的情况:

In my angular 8 application i create invoices.在我的 angular 8 应用程序中,我创建了发票。 this is an invoice object:这是一张发票 object:

{
"_id": {
    "$oid": "5ea9ad58f65d8d49841362bd"
},
"details": [
    {
        "_id": "5ea1eff27a1fcb29c4e7d1b6",
        "Client": "test",
        "km": 88,          
        "Subject": "test",
        "Location": "test",
        "StartTime": "2020-04-27T09:00:00.000Z",
        "EndTime": "2020-04-27T17:00:00.000Z",
        "IsAllDay": false,
        "StartTimezone": null,
        "EndTimezone": null,
        "Description": "oekfokef",
        "RecurrenceRule": "FREQ=DAILY;INTERVAL=1;COUNT=5;",
        "Id": 2,
        "CreatedBy": "bob"
    },
    {
        "_id": "5ea1f36297a9a315bc8ed078",
        "Client": "test",
        "km": 88,      
        "Subject": "ewfwefwe",
        "Location": "fwefwefwefewfwefwef",
        "StartTime": "2020-04-20T09:00:00.000Z",
        "EndTime": "2020-04-20T17:00:00.000Z",
        "IsAllDay": false,
        "StartTimezone": null,
        "EndTimezone": null,
        "Description": "wefwefewfwef",
        "RecurrenceRule": "FREQ=DAILY;INTERVAL=1;COUNT=5;",
        "Id": 3,
        "CreatedBy": "bob"
    },
    {
        "_id": "5ea1f38d97a9a315bc8ed083",
        "Client": "test2",
        "km": 38,
        "Subject": "test",
        "Location": "test",
        "StartTime": "2020-05-04T09:00:00.000Z",
        "EndTime": "2020-05-04T16:00:00.000Z",
        "IsAllDay": false,
        "StartTimezone": null,
        "EndTimezone": null,
        "Description": "test",
        "RecurrenceRule": "FREQ=DAILY;INTERVAL=1;COUNT=5;",
        "Id": 4,
        "CreatedBy": "bob"
    }
],
"client": "Robin",
"hoursWorked": 75,
"kmsTravelled": 880,
"invoiceDate": "2020-04-29T16:37:44.948Z",
"paid": "false",
"subTotal": 3917.2,
"travelexpenses": 167.2,
"tax": 879.7,
"hoursCosts": 3750,
"total": 4796.9,
"createdBy": "bob",
"__v": 0
}

But during the use of the application, certain properties change value, like hoursWorked , total , kmTravelled , hourCosts and details .但是在应用程序的使用过程中,某些属性值会发生变化,例如hoursWorkedtotalkmTravelledhourCostsdetails The updated objects get printed to the console.更新的对象被打印到控制台。 So whenever the user opens the component, i want it to post the whole object, but if an invoice with that Client name alread exists, only update those properties of each invoice per client .因此,每当用户打开组件时,我希望它发布整个 object,但如果已经存在具有该客户端名称的发票,则仅更新每个client每个发票的这些属性。

the updated object is this.invoice :更新后的 object 是this.invoice

this.invoice = {
      client: element.Client,
      hourCosts: (element.difference)*this.Client.price,

      hoursWorked: (element.difference),
      kmsTravelled: element.km,


      travelexpenses: this.Client.kmPrice* element.km,


      subTotal: (this.Client.kmPrice* element.km) + ((element.difference)*this.Client.price),
      total: ((this.Client.kmPrice* element.km) + ((element.difference)*this.Client.price)) + ((this.Client.kmPrice* element.km)+((element.difference)*this.Client.price) * this.tax/100),
      createdBy: this.userName,

      details: this.details
     }

So how do I go about this?那么我该如何处理这个问题呢? Sorry for a quite vague question, but i stuck with this quite a while now.很抱歉提出一个非常模糊的问题,但我现在坚持了很长时间。 If you need more info please let me know如果您需要更多信息,请告诉我

You could query by 'id' to search for specific values that have changed with something like: db.getCollection('hourCosts').find({_id:'5ea9ad58f65d8d49841362bd'})您可以通过 'id' 查询以搜索已更改的特定值,例如: db.getCollection('hourCosts').find({_id:'5ea9ad58f65d8d49841362bd'})

Searching in a collection by id with.find() will be efficient.通过 id with.find() 在集合中搜索将是有效的。 There's a really helpful mongoose doc page here .这里有一个非常有用的 mongoose 文档页面。

Also, just clarifying, you're posting the full customer schema if the name is unique, otherwise updating the relevant invoices by 'client', is that correct?另外,只是澄清一下,如果名称是唯一的,您将发布完整的客户模式,否则通过“客户”更新相关发票,对吗? The doc page linked should be useful for most query types and you can create additional mongoose schemas for some added granularity in what content you change.链接的文档页面应该对大多数查询类型很有用,您可以创建额外的 mongoose 模式,以增加您更改内容的粒度。

you can use mongoDB $set in the update command, example:您可以在更新命令中使用 mongoDB $set,例如:

db.city.update({_id:ObjectId("584a13d5b65761be678d4dd4")}, {$set: {"citiName":"Jakarta Pusat"}})

make sure you are passing the _id as ObjectId确保您将 _id 作为 ObjectId 传递

https://www.djamware.com/post/58578ab880aca715e80d3caf/mongodb-simple-update-document-example https://www.djamware.com/post/58578ab880aca715e80d3caf/mongodb-simple-update-document-example

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

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