1

With a document structure like:

{
_id:"1234",
values : [
    1,23,... (~ 2000 elements)  
]
}

where values represent some time series

I need to update some elements in the values array and I'm looking for an efficient way to do it. The number of elements and the positions to update vary.

I would not like to get the whole array back to the client (application layer) so i'm doing something like :

db.coll.find({ "_id": 1234 })

db.coll.update(
    {"_id": 128244 }, 
    {$set: {
        "values.100": 123,
        "values.200": 124
    }})

To be more precise, i'm using pymongo and bulk operations

dc = dict()
dc["values.100"]  = 102
dc["values.200"]  = 103

bulk = db.coll.initialize_ordered_bulk_op()
bulk.find({ "_id": 1234 }).update_one({$set:dc})
....
bulk.execute()

Would you know some better way to do it ?

Would it be possible to indicate a range in the array like (values from l00 to 110) ?

4
  • What's your MongoDB server version? Commented Jan 17, 2017 at 13:24
  • it is version 3.2.7 Commented Jan 17, 2017 at 13:41
  • would this answer your question: stackoverflow.com/questions/30124510/… Commented Jan 18, 2017 at 16:16
  • Thanks, it looks like we can filter the array elements via $exists but it does not look possible to use it to update part of the array Commented Jan 18, 2017 at 16:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.