I'm trying to code a projection in pymongo (3.3.0, Python 3.6.0, MongoDB 3.2).
The original data is approximately of the form:
{
"_id" : ObjectId("592fd5ac1aee05abb6104912"),
"Results" : {
"SomeData" : 1234,
"ResultAllPoints" : {
"MyArray" : [
{
"Value" : 0.5,
"Result" : 1,
},
{
"Value" : 1.5,
"Result" : 1,
}
{
"Value" : 1.7,
"Result" : 1,
}
]
}
}
}
I want to access the value stored in the field "Value" the second array entry of "MyArray" and use it as the value of a new field.
Using the MongoDB Shell, the command
db.my_collection.findOne().Results.ResultAllPoints.myArray[1].Value
gives me exactly the value I want to have in my resulting collection.
However, in my projection code, neither
{"newName" : "$Results.ResultAllPoints.myArray[1].Value"}
nor
{"newName" : "$Results.ResultAllPoints.myArray.1.Value"}
are working. In the first case, "newName" does not appear in the result at all the second case results in an empty array as the content of "newName".
I already know that I can use
{"$arrayElemAt": [ "$Results.ResultAllPoints.MyArray", 1]}
to access the Object containing the required information. But how can I access the content of "Value" afterwards?
Any help would be greatly appreciated! :)
EDIT: This is not a duplicate of Retrieve only the queried element in an object array in MongoDB collection, as I don't know the content of "Value" beforehand and therefore cannot use '$elemMatch'.