My model is
channel: [
{
type: ObjectId,
ref: 'Channel',
},
],
I need to push id inside it
await User.findByIdAndUpdate(
req.params.userId,
{
$push: {
channel: channel._id,
},
},
{
new: true,
}
).populate('channel')
But if i push in this way, the id is stored in ascending order, but i need to be stored in Descending order. I mean last to first.
CodePudding user response:
Try to $push
at the beginning of the array using the $position
modifier:
await User.findByIdAndUpdate(
req.params.userId,
{
$push: {
channel: {
$each: [channel._id],
$position: 0,
},
},
},
{
new: true,
}
);