6

For example, if I have some sensors and they have unique SN.

Then, I have some history data (not sorted) in this format: (SN, timestamp, value)

I want to maintain sensors' latest status in MongoDB. {"sn":xxx, "timestamp": xxx, "value": xxx, "installed_time": xxx}. Installed times might be manually added later.

So currently, my code is like

if db.sensors.find_one({"sn":SN, "timestamp": {"$lt": timestamp}}):
    db.sensors.update_one({"sn":SN}, {"$set":{"timestamp": timestamp, "value": value}}, {"upsert": True})

I'd like to know whether I can combine these to one operation.

I tried to do conditional upsert db.sensors.update_one({"sn":SN, "timestamp": {"$lt": timestamp}}, {"$set":{"timestamp": timestamp, "value": value}}, {"upsert": True}). The problem is that I'll have multiple documents with same SN.

For example, let's start with empty collection. First, (1, 3, 1) is processed, {"sn": 1, "timestamp":3, "value": 1} is inserted. Then, processing (1, 1, 2), it will create another document {"sn": 1, "timestamp":1, "value": 2}. The intended behaviour is just ignore this data point.

I also tried to do document replacement db.sensors.update_one({"sn":SN, "timestamp": {"$lt": timestamp}}, {"sn": SN, "timestamp": timestamp, "value": value}, {"upsert": True}). This will overwrite some other fields like installed_time.

Zixian Cai
  • 945
  • 1
  • 10
  • 17
  • can you post your some sample documents ? – Neo-coder Nov 22 '16 at 04:54
  • @Yogesh which part are you referring to? The history data is from, let's say mutiple different csv files, with columns SN, timestamp, value – Zixian Cai Nov 22 '16 at 05:08
  • I think it's look like your documents contains same SN values for same time stamp because of this you get multiple documents with same SN have you check this [findOneAndUpdate](https://docs.mongodb.com/v3.2/reference/method/db.collection.findOneAndUpdate/) – Neo-coder Nov 22 '16 at 05:14
  • @Yogesh No. Having multiple documents with same SN is bacause the data is not sorted. If I have document `{"sn": 1, "timestamp":3, "value": 1}` and then process `(1, 1, 2)`, it will create another document `{"sn": 1, "timestamp":1, "value": 2}` – Zixian Cai Nov 22 '16 at 05:17
  • See https://stackoverflow.com/a/64376376/397210 – levigroker Jun 10 '21 at 17:46

0 Answers0