I have multiple users in my collection and each user has an object of sessions shown below
{
user: 'adam',
sessions: {
key: some-value,
key: some-value
}
}
{
user: 'chris',
sessions: {
key: some-value,
key: some-value
}
}
I fetch the document on the basis of the user name. let us say i want the sessions of adam.
So I run db.collection.find({ user: 'Adam' }) which gives me full document of adam. Now I want the sessions of 'Adam' but the sessions object should be converted into an array of objects like this :
{
user: 'adam'
sessions : [ { key: value },{ key: value } ]
}
I want to convert the sessions of user into an object that means i want to do $objectToArray but on a specific username not the full collection.
Just the user I want.
Explanation:
I am running node as backend and i have a route called 'getuserdata' which gets data for a specific user based on the username. So when i will hit that route with username adam and it will get the data from the DB. The sessions of adam should come in an array of objects like shown above instead of an object which is in the db.
What i am doing
I am running a for-in loop on the back-end to convert them into array of objects but it is slow.