I recently started using MongoDB to save data from a project, the basic idea is the following.
On the server side I receive some JSON objects on a WebSocket and store it in the database.
The data I receive looks something like this:
{
{ident: "message_1"},
data:
[
{id: "data_1", value : 10},
{id: "data_2", value : 20},
{id: "data_3", value : 40},
{id: "data_4", value : 60},
{id: "data_4", value : 60},
],
timestamp : 12234456
}
And I'm currently saving the entire JSON object in the database.
The server also should handle user http requests where it queries the database for the requested data. The user requests are always for one element of the data array and the timestamp. Thus the server response should look like this:
{ id: "data_1", value : 10, timestamp : 12234456}
I tried using db.collection.find with $elemMatch but my output still contains the data array and the desired element.
{
data:
[{
id: "data_1",
value: 10
}],
timestamp : 12234456
}
So my question is if there's any possibility to query the database and return only the desired element of ´data´ array combined with the timestamp or I'll need re-create the response JSON object after reading the database.
Kind regards.