Hi all,
I am struggeling with building a query for my problem.
Example data:
{
"_id": {
"$oid": "123"
},
"id": "123",
"type": "CONFIGURATION",
"timestamp": {
"$date": {
"$numberLong": "1658189985472"
}
},
"installationId": "A",
"variables": {
"ONE": 7,
"TWO": 5,
"THREE": 10
}
},
{
"_id": {
"$oid": "123"
},
"id": "123",
"type": "CONFIGURATION",
"timestamp": {
"$date": {
"$numberLong": "1658189985662"
}
},
"installationId": "B",
"variables": {
"ONE": 6,
"TWO": 5,
}
},
{
"_id": {
"$oid": "123"
},
"id": "123",
"type": "CONFIGURATION",
"timestamp": {
"$date": {
"$numberLong": "1658189985662"
}
},
"installationId": "B",
"variables": {
"FOUR": 6
}
},
{
"_id": {
"$oid": "123"
},
"id": "123",
"type": "CONFIGURATION",
"timestamp": {
"$date": {
"$numberLong": "1658188874060"
}
},
"installationId": "B",
"variables": {
"ONE": 180,
"THREE": 10
}
}
I want to get for a given “installationId” (lets say “B”) the latest occurence of variables “ONE”, “TWO” and “THREE”.
I would need to get somthing similar to this:
[{'variable': 'ONE', 'value': 6, 'datetime': 1658189985662}, {'variable': 'TWO', 'value': 5, 'datetime': 1658189985662}, {'variable': 'THREE', 'value': 10, 'datetime': 1658188874060}]
I have a list of all the variables i want to get. Above example is just using dummy names. I am first testing the queries in MongoDB Compass, but have not yet found any way of doing the thing i want. Neither has Dr Google helped me, or i don’t know what to google/look for.
the following gives me all for a specific variable, but not the latest:
{installationId: 'B', {'ONE': {$exists: true}} }
I could add an $or to get also those for other variables, but not get only the latest for each var.
Perhaps someone here is willing to nudge me in the right direction :). I am not very skilled in this yet sadly.
Thanks in advance!
Greetings
Matt