0

I have this:

exports.deleteSlide = function(data,callback){
    customers.findOne(data.query,{'files.$':1},function(err,data2){

        if(data2){
            console.log(data2.files[0]);
            data2.files[0].slides.splice((data.slide-1),1);
            data2.files[0].markModified('slides');              


            data2.save(function(err,product,numberAffected){

                if(numberAffected==1){
                    console.log("manifest saved");
                    var back={success:true};
                    console.log(product.files[0]);
                    callback(back);
                    return;
                }
            });
        }
    });
}

I get the "manifest saved" message and a callback with success being true.

When I do the console.log when I first find the data, and compare it with the console.log after I save the data, it looks like what I expect. I don't get any errors.

However, when I look at the database after running this code, it looks like nothing was ever changed. The element that I should have deleted, still appears?

What's wrong here?

EDIT:

For my query, I do {'name':'some string','files.name':'some string'}, and if the object is found, I get an array of files with one object in it.

I guess this is a subdoc.

I've looked around and it says the rules for saving subdocs are different than saving the entire collection, or rather, the subdocs are only applied when the root object is saved.

I've been going around this by grabbing the entire root object, then I do loops to find the actual subdoc I that I want, and after I manipulate that, I save the whole object.

Can I avoid doing this?

  • Mongoose [automatically creates an _id field](http://mongoosejs.com/docs/guide.html#_id) if you don't specify one, so that shouldn't be the issue. – Matthew Bakaitis Mar 05 '14 at 17:40

1 Answers1

0

I'd probably just switch to using native drivers for this query as it is much simpler. (For that matter, I recently dropped mongoose on my primary project and am happy with the speed improvements.)

You can find documentation on getting access to the native collection elsewhere.

Following advice here: https://stackoverflow.com/a/4588909/68567

customersNative.update(data.query, {$unset : {"slides.1" : 1 }}, function(err){
  if(err) { return callback(err); }

  customersNative.findAndModify(data.query, [],
   {$pull: {'slides' : null } }, {safe: true, 'new' : true}, function(err, updated) {
  //'updated' has new object
} );
});
Community
  • 1
  • 1
Will Shaver
  • 12,471
  • 5
  • 49
  • 64
  • Where in this code would I modify the object? After the `update` call or after the `findAndModify` call? And how would I save it? Also, by "native drivers" do you mean stock mongodb functionality, or something else? –  Mar 05 '14 at 22:16
  • Your goal is to remove an item from the slides array right? This code removes the item in the '1' position. – Will Shaver Mar 05 '14 at 22:28
  • Oh, okay. I thought that bit might have meant "return one element of `slides`" or something. But why is the `findAndModify` call necessary? Does `update` save on the database? Should I call `save` on the `updated` object? And lastly, what do you mean by "native drivers"? –  Mar 05 '14 at 22:50