I have a document with a Array called ‘lastLogin’ which has all the recent timestamps.
I would like to overwrite this array with a single element being the last index value in the array.
from
{
lastLogin : [timestamp1,timestamp2,timestamp3]
}
to
{ lastLogin : timestamp3 loginTimes : 3
}
So overwrite the lastLogin array with a single value of the last index
Add a field called loginTimes which is the total count of the lastLogin Array. (edited)
Not sure exactly how to do this either using aggregation or normal commands.
Test it before running in prod because it overwrites the lastLogin field as you requested. But you could also write the $last value into another field just to be safe if you prefer.
Humm maybe but I’m not sure. At least this is its equivalent and I don’t know if the version without the aggregation pipeline is possible. At least I have no idea how I would do it if it’s even possible. Why is this important?
Then this course was especially designed for you! And it’s free !
I don’t even know how to reuse the value of an existing field in the document without the aggregation framework. Maybe there is a way but the aggregation framework is really like super powers!
Using MongoDB without the aggregation framework is like using a motorbike without putting gas in it. ! You will eventually be able to get to your destination but this is going to require a lot more effort and it won’t be efficient .
I did some research before sending this answer and there is a way to do this with an “update” operation. But you will also need the aggregation pipeline which can now be used in the update operations… . So it’s aggregation or aggregation basically.