1

I have objId($oid) of the document and I am trying to remove the document in Sails by using destroy function

User.destroy({id:objId}).exec(cb);

It is not working. I tried suggestion from other post. But it does not work either.I tried using Mongo ObjectId function too but it does not work either. Any suggestions. I can use native function but prefer not to.

Community
  • 1
  • 1
Spatil
  • 81
  • 4
  • Can you provide any more information, such as an error received? Try `User.destroy(objId).exec(function(err, destroyed){ if(err) {...} else {...} });` –  Dec 17 '15 at 17:45
  • @metzuda I tried User.findOne(objId).exec(function(e,r){console.log(e,r)}) to see if passing just objId works. Does not show any error. Prints null and undefined. – Spatil Dec 22 '15 at 10:13
  • if `r` is returning undefined then that usually means it doesn't exist in the database. –  Dec 22 '15 at 10:16
  • Objid I am trying to query does exist. I tried User.find({id:User.mongo.objectId(objId)}).exec(function(e,r){console.log(r)). It returns entire user collection instead of specific user. Do I have to use _id? – Spatil Dec 22 '15 at 15:18

2 Answers2

0

In your model try to change the type of id as objectid and hope it works

module.exports={
    id : {
       type : objectid
    }
}
selftaught91
  • 7,013
  • 3
  • 20
  • 26
0

To use ObjectId in any operation with mongoDb , you can try to load the native lib first as this example

var ObjectId = require('mongodb').ObjectID;

User.native(function (err, collection) { collection.update({_id: new ObjectId(anIdVariable)},{$push:{pubs:aPubsVariable}}, function (err) { });

Works for me also to remove document :

collection.deleteOne({ _id :ObjectId("5920263cce3510410d492489") }, function(err, result) { }

Fabien Thetis
  • 1,666
  • 15
  • 11