I have two mongodb operations which I currently accomplish with two calls to the collection.
await req.db.collection('likes').updateOne(
and
await req.db.collection('likes').countDocuments(
Note that the count happens after the update operation.
How to combine these into one?
handler.put(async (req, res) => {
const {userLiked, userLiking, liked } = req.body;
await req.db.collection('likes').updateOne(
{
_id: userLiking + "_" + userLiked,
"from": userLiking,
"to": userLiked,
},
{
$set: {
like: liked,
updated: new Date(),
},
},
{upsert: true}
);
let oppositeLikeStatus = await req.db.collection('likes').countDocuments(
{
_id: { $in: [userLiked + "_" + userLiking, userLiking + "_" + userLiked]},
like: true,
},
);
setTimeout(() => {
if (oppositeLikeStatus > 1) {
res.json(true);
}
else {
res.json(false);
}
}, 10);