Deleting a subdocument in mongo not working

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Deleting a subdocument in mongo not working



I have a Node/Express app where I am trying to delete an embedded subdocument with the $unset operator in a findAndUpdate function. But nothing seems to be happening. I know the filter is correct.



Here is an example of the object, where the embedded subdocument I'm trying to clear is the "shared" object.



"_id" : ObjectId("5b6cf8341666b48b847ab956"),
"emails" :
"email_type" : "home",
"email_address" : "tom@jones.com",
"_id" : ObjectId("5b6cf834ee47267affac2c89")
,
"owner_id" : "5b6b2ee79076c830f01145c1",
"__v" : 0,
"first_name" : "Tom",
"last_name" : "Jones",
"initial" : "",
"accepted" : true,
"shared" :
"emails" : [

"email_type" : "personal",
"email_address" : "randy@pits.com",
"_id" : ObjectId("5b6b2f419076c830f01145c5")

],
"phones" : [

"phone_number" : "2065567876",
"phone_type" : "home",
"_id" : ObjectId("5b6b2f289076c830f01145c3")
,

"phone_number" : "4256788765",
"phone_type" : "mobile",
"_id" : ObjectId("5b6b2f399076c830f01145c4")

],
"addresses" : [

"address" : "19889 West Palm",
"city" : "Seattle",
"state" : "State",
"zip" : "98999",
"address_type" : "personal",
"_id" : ObjectId("5b6b2f5b9076c830f01145c6")

],
"businesses" : [ ],
"_id" : ObjectId("5b6cf834ee47267affac2c88"),
"invite_id" : "TomJones5b6b2ee79076c830f01145c1",
"first_name" : "Randy",
"last_name" : "pits"
,
"share_id" : "5b6cf834ee47267affac2c8b"



and here is my function:


const shareId = contact.share_id;

Contact.findOneAndUpdate(_id:shareId,
$unset:
"shared":""

)



Not sure what I could be doing wrong...



Update: Thought I'd share the whole block. I've tried all the suggestions, but it just doesn't work.



I don't get what I'm missing:


router.route('/deletecontact/:contactId')

.delete(function(req, res)
Contact.findOne( _id: req.params.contactId , function(err, contact)
if (err)
res.send(err);

const shareId = contact.share_id;
console.log('Contact ID is: ' + contact._id);
console.log('Contact share ID is: ' + shareId);

// Contact.findOne(_id:shareId, (err, sender) =>
// console.log('The Sender: ', sender);
// )

Contact.update(_id:shareId, $unset:shared:1);


res.json( 'msg': 'Successfully removed contact' );

);
);



Thanks!





try $unset: shared:1
– Akrion
Aug 10 at 3:47



$unset: shared:1





@Akrion, Thanks but that didn't work.
– cnak2
Aug 10 at 3:57





your code is working for me.... Try with hardcoded shareId
– Anthony Winzlet
Aug 10 at 4:50


shareId




1 Answer
1



Try using the update since it would only change one doc anyway.


update



This mongoDB query works (tested it):


db.getCollection('<TEST-COL>').update(
_id: ObjectId('<YOUR-OBJ-ID')
,
$unset:
shared: 1

)



Now although that works in Robo 3T and MongoDB console it seems that Mongoose would like you to do something like this:


Contact.findOne('_id': mongoose.Types.ObjectId('<YOURID>'), function(err, model)
model.set('shared', undefined)
model.save()
)



Now as per their docs this seems to be the overall recommended way due to:



Also note: although values are casted to their appropriate types when
using update, the following are not applied: defaults, setters, validators, middleware triggered on save





Just added the end-point in full in the original post. I've tried all the suggestions, but it just doesn't work.
– cnak2
Aug 10 at 6:33





Updated my answer for mongoose specifically. Let me know if this works
– Akrion
Aug 10 at 7:28






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard