1

Im working with node.js and mongoose.

The task is as follows: I need to update the information in the database, provided that the required field in the database does not exist or its value is less than 'x'.

In more detail, this will be information about the players in the database, each of them will have a "Records" object in which there will be different fields. And I need to update the information of these fields, if the information received is greater than that which exists in the database.

How can i do it?

Thanks!

Ariqun
  • 13
  • 4

1 Answers1

0

I do not think you can create a column without having it in the schema, I would suggest you have an array of Objects in your schema that you can push values into if it does not exists and update the value if the new value is greater Check this out: How to push an array of objects into an array in mongoose with one call?

fatiu
  • 1,502
  • 1
  • 8
  • 14
  • 1
    Yea! I have `"records: Array"` in the schema. And i can add new fields as `['records.${field}']: value`. But I do not know how to do this, provided that the input value is greater than what is in the database. – Ariqun May 05 '21 at 08:51
  • The query should look like this db.inventory.update( { "carrier.fee": { $gt: 2 } }, { $set: { price: 9.99 } } ) Check this link for detailed Doc https://docs.mongodb.com/manual/reference/operator/query/gt/ – fatiu May 05 '21 at 08:58
  • You can paste your existing query for an edit – fatiu May 05 '21 at 09:05
  • 1
    Thank you very much, you saved hours of my life! – Ariqun May 05 '21 at 09:14