1

I'm trying to update a field seconds_until_start by a calculation of two other fields. This doesn't seem to work in mongodb in the way that seems most intuitive. Any suggestions what else I can do are appreciated.

db.price_scrape.update({"timestamp": {"$exists": true}},
  {"$set": {
      seconds_until_start: {$divide: [{$subtract: ["$marketstarttime", "$timestamp"]}, 1000]}
  }},
multi=true)

The dollar ($) prefixed field '$divide' in 'seconds_until_start.$divide' is not valid for storage.

Nickpick
  • 6,163
  • 16
  • 65
  • 116
  • 4
    [`$divide`](https://docs.mongodb.com/manual/reference/operator/aggregation/divide/) is an aggregation operator, you can't use it in an `update`. See [here](https://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field) for the options for using other fields' values in an update. – JohnnyHK Mar 10 '18 at 01:31
  • it doesn't really answer the question in your link as it only composes a new field from two others, but without any operator – Nickpick Mar 10 '18 at 19:43
  • Short answer is that it isn't directly supported so you need to use one of the approaches in the link based on your situation. – JohnnyHK Mar 11 '18 at 00:20

1 Answers1

0

Check your mongo version, if it's not v4.2+ your query won't work.

As @JohnnyHK specified in the comment (this link)

Check the first answer which gives an insight into how you can write your query in lower versions on Mongo DB

Amit Beckenstein
  • 1,220
  • 12
  • 20
Lokesh sp
  • 81
  • 2
  • 6