1

So this is my case:

In my angular 8 application i create invoices. this is an invoice 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. 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.

the updated object is 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

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Y_Lakdime
  • 825
  • 2
  • 15
  • 33

2 Answers2

0

You could query by 'id' to search for specific values that have changed with something like: db.getCollection('hourCosts').find({_id:'5ea9ad58f65d8d49841362bd'})

Searching in a collection by id with .find() will be efficient. There's a really helpful mongoose doc page here.

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.

Thomas
  • 39
  • 1
  • 5
0

you can use mongoDB $set in the update command, example:

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

make sure you are passing the _id as ObjectId

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

Ahmad Alinat
  • 141
  • 8