So what I’m trying to do, if you take a look at the picture bellow, is to delete just one document inside transactions
by it’s id.
What I have tried:
const result = await db.updateMany({ pipedrive_id: 999 }, { $pull: { transactions: { id: id } } });
As suggested in this stackoverflow post. But it didn’t do anything.
Then I tried:
const result = await db.findOneAndDelete(
{
'transactions._id': { $in: id },
}
);
Where id
in both cases is the _id
of the transaction inside transactions
. But this didn’t work as expected as it deleted everything all items in the collection and the parent as well.
So I’m kinda stuck, any help?
2
Answers
So, I kinda achieved my goal, but I'm betting there's proper mongodb solution to this. This is just hacky way to do it I guess.
The problem is the answer you are trying to follow doesn’t works. (Also the comment in the answer is correct). Check this example and how it fails.
As explained, for example in the Node.JS Driver Jira Ticket $pull operator doesn’t work with dot notation
So you need this query:
Note how in the
$pull
object there is not dot notation, is defined the object itself.Example here