First of all I'm sorry if my english is not very good, I hope what I'm going to write will be understandable.
I have a document with this schema:
{
"fields":[
{"field": field1, "value": 1},
{"field": field2, "value": 2},
{"field": field3, "value": 3}
],
"time": datetimeObj
},
{
"fields":[
{"field": field1, "value": 4},
{"field": field4, "value": 5}
],
"time": datetimeObj
}
What I'm trying to do is to produce a query that returns the list of values of one specific field.
For example if I want the values of the field "field1" I expect something like:
[1, 4]
For field "field2":
[2, 0] // 0 because it doesn't exist in the second document
Now I'm trying to obtain this result using aggregate operator like this:
db.collection.aggregate([
{
$project: {
value: {
$filter: {
input: "$fields",
as: "fields",
cond: { $eq: [ "$$fields.filed", "filed1" ] }
}
},
_id : 0
}
}
But what I get is much verbose than I'd like to:
{
"value" : [
{
"value" : NumberInt(1),
"filed" : "field1"
}
]
}
{
"value" : [
{
"value" : NumberInt(4),
"word" : "field1"
}
]
}
Is there a way to get the result just as an array of values? Also, is it possible to sort these values by the time field?
I'm using the python library, so it would be very usefull to have a python example of the solution. Thank you