简体   繁体   中英

MongoDB auto-increment key in Document C#

I want to test Real Time Data Series performance using MongoDB, but I'm getting some trouble while updating the document.

I want something like the following structure:

{
 timestamp_minute: ISODate("2013-10-10T23:06:00.000Z"),
 type: "memory_used",
 values: {
   0: 999999,
   …  
   37: 1000000,
   38: 1500000,
   … 
   59: 2000000
 }
}

The thing is that I can't seem to update the "values" BsonDocument accordingly because both the key and value are integers. I've tried inc but no luck.

Any help would be appreciated! Thanks.

I see want you want to do, update after reading you comment. There are various ways of doing :)

From the blog you mention in the comment, what you do is create a single document with an array size of 60 (every second), that way you can update your document with :

  db.metrics.update(
  { 
    timestamp_minute: ISODate("2013-10-10T23:06:00.000Z"),
    type: ”memory_used”
  }, 
  {$set: {“values.59”: 2000000 } }
)

Where .59 is the second you want to update. so just call the update every second when upsert is set to true the first update creates the document

When using c# as mentioned by you you could do something like:

if (second==0){
//insert the "new" timestamped object into the collection
}
else{
var update = Builders<Metric>.Update.Set(e => e.values[second], this.getRandomMetric());
}

This should give you a document with a value array of 59 items.

But for reporting it's not really a go i think, why not just store it in a single document?

{
     timestamp_minute: ISODate("2013-10-10T23:06:01.000Z"),
     type: "memory_used",
     value: 999999
 },
 {   
     timestamp_minute: ISODate("2013-10-10T23:06:37.000Z"),
     type: "memory_used", 
     value: 1000000
  },
     timestamp_minute: ISODate("2013-10-10T23:06:59.000Z"),
     type: "memory_used",
     value: 2000000
  }

If you are interested in limited history just put it in a capped collection . You can always group the averages and put them in an other collection. For this you can use the aggregation framework with the $out operator . For example group the values per type per hour or something like that, whatever fits your needs ;-).

So I figured it out, not really sure if it is the best approach but it works. Hope it helps someone too.

var update = Builders<Metric>.Update.Set(e => e.values["0"], this.getRandomMetric()).Inc(e => e.numberOfRecords, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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