0

I am trying to delete one array element when I click delete button on jade view page.

When clicked, it's going to send selected instructor objected as req.body.

At sever side, it will find courses that contain the instructor objectId.

Any idea for me?

Thank you for reading it.

here is my code:

var id = req.body._id;
clist.find({ instructors: { $in: [id] } }).exec(function (err, result) {
            result.forEach(function (obj) {
                clist.update(
                    { _id: new mongoose.Types.ObjectId(obj._id)},
                    { $pull: { instructors : [new mongoose.Types.ObjectId(id)] } }
                );
                console.log(new mongoose.Types.ObjectId(obj._id) + ' was deleted');
            });
        });

Schema Clist and ilist:

var instructorlist = mongoose.Schema({
    name: { type: String, required: true },
    age: { type: Number, required: true },
    gender: { type: String, required: true },
    DOB: { type: Date, required: true, default: Date.now },
    email: { type: String, required: true },
    phone: { type: Number, required: true },
    address: { type: String, required: true },
    dateofstart: { type: Date, required: true},
    courses: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "clist"
    }]
});
var courselist = mongoose.Schema({
    coursename: { type: String, required: true },
    coursenumber: { type: String, required: true },
    coursecredit: { type: Number, required: true },
    courseroom: { type: String, required: false },
    courseregisteddate: {type: Date, default: Date.now},
    students: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "slist"
    }],
    instructors: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "ilist"
    }]
});

one example for mongodb :

{
    "_id": {
        "$oid": "591a7a3b391a1842e8a69e23"
    },
    "coursename": "JDKD",
    "coursenumber": "COMP4483",
    "coursecredit": 4,
    "courseroom": "sdaf",
    "instructors": [
        {
            "$oid": "591a374422a3a13d38c0bbe5"
        }
    ],
    "students": [],
    "courseregisteddate": {
        "$date": "2017-05-16T04:04:11.848Z"
    },
    "__v": 0
}

When I add instructor objectID in Course.

var newcourse = new clist({
                    'coursename': req.body.coursename, 'coursenumber': req.body.coursenumber, 'coursecredit': req.body.coursecredit
                    , 'courseroom': req.body.room, 'instructors': instructors._id
                });     
4
  • Can you post the schema for whatever becomes the 'clist' model? Commented May 16, 2017 at 5:19
  • @Paul I updated my posting. Commented May 16, 2017 at 5:22
  • Why you using loop there ? Commented May 16, 2017 at 5:25
  • @Love-Kesh because one instructor would have more than one course Commented May 16, 2017 at 5:25

1 Answer 1

1

Use same operation to find and update multiple

clist.update(
  { instructors: { $in: [id] }},
  { $pull: { instructors : { _id : new mongoose.Types.ObjectId(id) } } }, //or{ $pull: { instructors: mongoose.Types.ObjectId(id) } }
  {
   multi:true
 },
 function(error, success){
  if(error){
   console.log("error",error)

 }
 console.log("success",success)

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

7 Comments

success { ok: 1, nModified: 0, n: 2, opTime: { ts: Timestamp { bsontype: 'Timestamp', low: 3, high_: 1494912734 }, t: 1 }, electionId: 7fffffff0000000000000001 }
Thank you very much. I will try it!
if try { $pull: { instructors : { _id : new mongoose.Types.ObjectId(id) } } } then { $pull: { instructors : { $oid : new mongoose.Types.ObjectId(id) } } }
Try this { $pull: { instructors : { _id : id } } }
tried { $pull: { instructors : { _id : id } } }, { $pull: { instructors : id } }, { $pull: { instructors : mongoose.Types.Objectid( id) } } } but same result like success something.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.