'Create entire new document or update existing in mongodb using mongoose (nodejs)
Hi all I am trying to create a new document or update it if it exists
here is the code
if (!isMemberr) {
const newSubscription = new membership({
user_id: req.body.user_id,
plan_id: req.body.plan_id,
transaction_id: req.body.transaction_id
});
newSubscription.update({ upsert: true, setDefaultsOnInsert: true }).then(() => {
res.status(201).json({
Status: 201,
Message: "Subscription Created",
Data: newSubscription
})
})
const filter = { _id: req.body.user_id };
const update = { member: true };
let updatedStatus = await User.findOneAndUpdate(filter, update);
updatedStatus.member;
plan()
the issue is if a particular document already exists then it is updating it but when I delete the document and call the API it is not giving any error or does not create a new document.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|