0
//MONGOOSE SCHEMA OBJECT
var userSchema = new Schema( {
  username: { type: String, required: true, unique: true },
  email: { type: String, required: true, unique: true },
  tags:[ {name : String, color : String } ],
  bookmarks:[{link : String, tags:[ {name : String, color : String } ]}]
} );

module.exports = userSchema;        //Export the userSchema
var UserModel = mongoose.model('UserModel', userSchema ); //Create Model
module.exports = UserModel;      //Export the Model

//I CAN DELETE AN ITEM FROM BOOKMARKS ARRAY NO PROBLEM USING

UserModel.findByIdAndUpdate(userId ,{$push : {bookmarks: {link : req.body.link, tags : req.body.tags}}}, function(err, user_data) {

PROBLEM!! How do I delete a tag from the tags Array, within the bookmarks array given users _id, bookmarks _id and the tag _id or name?

//I have tried variations of the following without success

var update = {bookmarks:[{ _id : bookmarkId},
$pull: {tags:[_id : tagid ] }] };

UserModel.findByIdAndUpdate(userId ,update, function(err, user_data) {

AND

UserModel.findOne( {_id : userId}).select({ bookmarks:[ { $elemMatch: {_id : req.params.bookmarkId}}] }).exec(function(err, user_data)

Initially I was using different Models and subdocuments.

var bookmarkSchema = new Schema( {
    link : String,
    tags:[tagSchema]
});

var tagSchema = new Schema( {
    name : String,
    color : String
});

var userSchema = new Schema( {
  username: { type: String, required: true, unique: true },
  email: { type: String, required: true, unique: true },
  tags:[ {name : String, color : String } ],
  bookmarks: [bookmarkSchema]
} );

However, I was unable to delete items from the subdocuments using the $pull command like I used above. So I reverted to just using one schema/model. This is a very important task to be able to complete and I would be greatful for help.

Thanks Muhammad but I could not get either of the 2 methods to work: 1) Nothing happens to DB and the values of the callbacks are: *numberAffected: 0 *raw: {"updatedExisting":false,"n":0,"connectionId":322,"err":null,"ok":1}

UserModel.update({bookmarks:req.params.bookmarkId},
    { $pull: {"bookmarks.tags" :{_id:req.body._id, name :req.body.name ,color:req.body.color}}}, function(err, numberAffected, raw) {

2) I had to use the lean() function to convert Mongoose Document data format to normal JSON. Otherwise bookmarks.push({link:user.bookmarks[i].link,_id:user.bookmarks[i]._id,tags:tags})

would not combine properly with:

bookmarks.push(user.bookmarks[i]);

on bookmarks[]

However using the lean() functions means I would not be able to save the data to the DB with .save

UserModel.findById(userId).lean(true).exec(function(err, user) {

1 Answer 1

0

delete from sub-Document of JSON doc,

UserModel.update({bookmarks:bookmarkId},{$pull: {"bookmarks.tags" :{_id:tagsId,name :name ,color:color}}}, function(err, user_data) {

or

UserModel.findOnd(userId,function(er,user){
 var bookmarks= [];
 var tags = [];
 for (var i = 0;i<user.bookmarks.length;i++){
   if (user.bookmarks[i]._id==bookmarkId)
   {
    for (var j = 0;j<user.bookmarks[i].tags.length;j++){
      if (user.bookmarks[i].tags[j]._id==tagid )
      {

      }else {
         tags.push(user.bookmarks[i].tags[j])
       }
     }
    bookmarks.push({link:user.bookmarks[i].link,_id:user.bookmarks[i]._id,tags:tags})
   }
   else {
       bookmarks.push(user.bookmarks[i]);
        }
 }

})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.