I've encountered the following issue:
let post = await User.findOneAndUpdate(
{ _id: USER_ID },
{
$push: {
posts: {
title,
content,
userId: USER_ID,
titleUrl,
thumbnail: {
webp: WEBP_PATH,
jpeg: JPEG_PATH,
placeholder: PLACEHOLDER
}
}
}
},
{ new: true }
);
This query is mostly functional. However, my problem arises when pushing an item to the array as I want only the updated element returned. Instead, I'm receiving the entire document with all posts in the array. While I could use something like posts.slice(-1)
, it seems quite inefficient to load everything when I only require one element, doesn't it?
Does anyone know of a way to retrieve just the newly added object?