0

I'm using Waterline in my Sails.js app, with the sails-mongo adapter. I'm trying to get a record from my MongoDB searching by _id. This is what I've tried:

Case.findOne({id: args.itemId}).exec(function(err, retObj) {...}
Case.findOne({_id: args.itemId}).exec(function(err, retObj) {...}
Case.findOne({_id: { 'contains': args.itemId} }).exec(function(err, retObj) {...}
Case.findOne({_id: { 'like': '%' + args.itemId} }).exec(function(err, retObj) {...}

and none of those return an object. The only thing I've found that returns an object is:

Case.findOne({_id: { '!': args.itemId} }).exec(function(err, retObj) {...}

which I found in this StackOverlow answer. But...that is the not operator. So while it worked when I only had one record in the collection, it doesn't return the correct object. I'm not sure why it even worked at all when there was one object in the collection to begin with.

What do I need to do to get an object by its ID with sails-mongo?

Community
  • 1
  • 1
pjlamb12
  • 2,300
  • 2
  • 32
  • 64

1 Answers1

0

After more digging and looking, Sails.js Attributes documentation has an 'objectid' type, and once I set that in my Sails.js model file, the find worked perfectly.

I ended up with this query:

Case.findOne({id: args.itemId}).exec(function(err, item) { ... }

Hope this helps others!

pjlamb12
  • 2,300
  • 2
  • 32
  • 64