I would like to alter a document in MongoDB. Below is an example extract of the relevant document structure:
{ values : { M1 : [[1395964800, 0.083434], ... , [1395964860, 0.043424]] } }
Firstly, key M1
contains an array of arrays. I must search over the [0]
item in the nested arrays (epoch time, e.g. 1395964800, 1395964860
). How do I find a document by [0]
nested array value? For example, I have tried searching:
db.myCollection.find({ "values" : { "M1" : [1395964800, 0.083434] } }).limit(1);
Admittedly, I expected this approach would search by both the [0]
nested array value && the [1]
nested array value. Even still, this does not work, and returns no results (but does not error).
Secondly, if this [0]
array value already exists (epoch time) I would like to update/overwrite the [1]
element in that nested arrays (data value). If the [0]
array value does not exist within the M1 array, I would like to add it (and an accompanying data value) to the M1 array.
I expect I would use something similar to this:
db.collection("myCollection").findAndModify(
{ ........ }, // query
{ }, // sort
{ $set : { ... : ... }}, // set
{ upsert: true }, // insert if does not exist
function (err, result) {
(err) throw err
}
);
Questions
- How to search as described
- How to update as described