title: remove by _id in mongodb doesn't work
body: <p>I originally thought it would be really simple to create an delete function to use with my mongodb but am unable to get it to work and haven't been able to find a solution on google or SO. </p>
  
  <p>I'm creating an application using the MEAN stack that uses Users and Courses and have used an example to do the User-handling part so the delete function works for the users. When I tries to duplicate it so that courses will have one as well I can't get it to work. </p>
  
  <p>Have attached all (relevant) code I use for creating the service. Some of it may not be needed to solve the problem and is more related to how the information is communicated within the app. Let me know if anything else is required. </p>
  
  <p><strong><em>Client-side code</em></strong></p>
  
  <p><strong>admin/index.controller</strong></p>
  
  <pre><code> function deleteCourse() {
      // CourseService calls app-services/course.service to enable 
     // all sub-pages to call all course-functions. 
      CourseService.Delete(event.target.id)
          .then(function(){
               initController();
              })
          .catch(function(error){
              });
  
      }
  </code></pre>
  
  <p><strong>app-services/course.service</strong></p>
  
  <pre><code>function Delete(_id) {
      return $http.delete('/api/courses/' + _id).then(handleSuccess, handleError);
      }
  </code></pre>
  
  <p><strong><em>Server-side code</em></strong></p>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>app.use('/api/courses', require('./controllers/api/courses.controller.js'));
  </code></pre>
  
  <p>I think the error is either in the following file, or in services/course.service.js which is presented a bit further down. </p>
  
  <p><strong>courses.controller.js</strong></p>
  
  <pre><code>var config = require('config.json');
  var express = require('express');
  var router = express.Router();
  var CourseService = require('services/course.service');
  
  router.delete('/:_id', deleteCourse);
  
  module.exports = router;
  
  
  function deleteCourse(req,res) {
  //Do not now why req.user.sub works ( it passes on the ID). 
  //Copied it from the corresponding function for User with the intention to change it to req.course.sub but that didn't work. 
  //Somehow req.user.sub did to let it stay. Might cause the error? 
  
  var courseID = req.user.sub;
  CourseService.delete(courseID)
     .then(function(){
         res.sendStatus(200);
     })
     .catch(function (err) {
         res.status(400).send(err);
     });
  }
  </code></pre>
  
  <p><strong>services/course.service.js</strong></p>
  
  <pre><code>var config = require('config.json');
  var _ = require('lodash');
  var jwt = require('jsonwebtoken');
  var bcrypt = require('bcryptjs');
  var Q = require('q');
  var mongo = require('mongoskin');
  
  
  //Connectionsstring to the mongo database, if the app is runed on openshift the connectionstring value will change.
  var connectionString = "mongodb://localhost:27017/mean-stack-registration-login-example";
  if(process.env.OPENSHIFT_MONGODB_DB_URL){
      connectionString = process.env.OPENSHIFT_MONGODB_DB_URL + "studycontrol";
  }
  var db = mongo.db(connectionString, { native_parser: true });
  
  db.bind('courses');
  
  var service = {};
  service.delete = _delete;
  module.exports = service;
  
  // this is where I think the error occurs. this is the current version of it,       will include examples of what i've tried earlier below. Current version corresponds with the function for deleting users. 
  
  function _delete(_id){
      var deferred = Q.defer();
      db.courses.remove(
          { _id: mongo.helper.toObjectID(_id) },
          function (err) {
              if (err) deferred.reject(err);
  
              deferred.resolve();
          });
      return deferred.promise;
  }
  </code></pre>
  
  <p><strong>Examples of what I've tried in the delete function above</strong></p>
  
  <pre><code>// function(err) has been pretty much the same as for the delete function above for all tries. 
  
  db.collection('courses').remove({"_id": ObjectID(_id)}, function(err)...)
  
  db.courses.remove({"_id": "ObjectId"(_id)}, function (err)... )
  
  db.courses.remove({"_id": "$oid":(_id)}, function(err)... )
  
  same as above but without " " around _id, ObjectID and $oid
  </code></pre>
  
  <p><strong>Deleting function for users in services/user.service.js for comparison</strong></p>
  
  <pre><code>var config = require('config.json');
  var _ = require('lodash');
  var jwt = require('jsonwebtoken');
  var bcrypt = require('bcryptjs');
  var Q = require('q');
  var mongo = require('mongoskin');
  
  
  //Connects to the database with diffrent connectionstring based on if you connect localy or in openshift
  var connectionString = "mongodb://localhost:27017/mean-stack-registration-login-example";
  if(process.env.OPENSHIFT_MONGODB_DB_URL){
      connectionString = process.env.OPENSHIFT_MONGODB_DB_URL + "studycontrol";
  }
  var db = mongo.db(connectionString, { native_parser: true });
  
  db.bind('users');
  
  var service = {};
  service.delete = _delete;
  module.exports = service;
  
  //Deletes the user form the database based on ID.
  function _delete(_id) {
      var deferred = Q.defer();
  
      db.users.remove(
          { _id: mongo.helper.toObjectID(_id) },
         function (err) {
              if (err) deferred.reject(err);
              deferred.resolve();
          });
      return deferred.promise;
  }
  </code></pre>
  
  <p><strong>EDIT</strong> Have been trying suggestions by Sundar and anwerjunaid and decided to use some console.log to try and figure out what's going on. </p>
  
  <p>below is one example that i've tried and the log I got </p>
  
  <pre><code>db.courses.remove(
          { "_id": _id},
          function (err) {
              if (err) deferred.reject(err);
              deferred.resolve();
              console.log('inside')
              console.log(deferred);
          });
     console.log('outside')
     console.log(deferred)
     return deferred.promise;
  
  
  outside
  defer {
      promise: { state: 'pending' },
      resolve: [Function],
      fulfill: [Function],
      reject: [Function],
      notify: [Function] }
  inside
  defer {
    promise: { state: 'fulfilled', value: undefined },
    resolve: [Function],
    fulfill: [Function],
    reject: [Function],
    notify: [Function] }
  </code></pre>
  
  <p>Does this mean that it executes return.deferred.promise before db.courses.remove is complete? Could that be what is causing the error? </p>