2

I am not able find the record using Model.find(id).

Food.find('548210e8d5a81037af06b2d6') => Mongoid::Errors::DocumentNotFound

But when I try find the same record using column name , I will return same record.

 Food.where({name:"Aloo Matar" }).first
 => #<Food _id: 548210e8d5a81037af06b2d6, rawOrPrepared: "P", name: "Aloo Matar", tags: "vegetable", alternateNames: "potatoes">

For my case, find works differently based on string. Please see the below code.

  Food.where({_id: "zyCMnbTPENeXkhawT" })
 => #<Mongoid::Criteria
  selector: {"_id"=>"zyCMnbTPENeXkhawT"}
  options:  {}
  class:    Food
  embedded: false>

2.2.1 :017 > Food.where({_id: '548210e8d5a81037af06b2d6' })
 => #<Mongoid::Criteria
  selector: {"_id"=>BSON::ObjectId('548210e8d5a81037af06b2d6')}
  options:  {}
  class:    Food
  embedded: false>

But first code returns the object while second code raise the exception.

Please help me on this.

Thanks, Hare

Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
Hare Ram
  • 743
  • 2
  • 7
  • 19
  • Is the `_id` a string that looks like a `BSON::ObjectId` or a real `BSON::ObjectId`? What does `db.foods.findOne({ _id: '548210e8d5a81037af06b2d6' })` say from the `mongodb` CLI tool? How about `db.foods.findOne({ _id: ObjectId('548210e8d5a81037af06b2d6') })`? – mu is too short Feb 26 '16 at 15:30
  • from mongo console, i am able to find the data. Please see the below code from mongo client. db.foods.find({_id: "548210e8d5a81037af06b2d6"}) { "_id" : "548210e8d5a81037af06b2d6", "manufacturer" : "homemade", "modified" : "2014-12-10T06:47:56.275Z", "name" : "Aloo Matar"} > > db.foods.find({_id: "zyCMnbTPENeXkhawT"}) { "_id" : "zyCMnbTPENeXkhawT", "name" : "Recipe_281015072818", "tags" : "recipe", "manufacturer" : "homemade", "alternateNames" : "Brown Rice and Carrots", "modified" : "2015-10-28T07:28:20.542Z" } – Hare Ram Feb 26 '16 at 15:41
  • I can get these data from mongodb client. – Hare Ram Feb 26 '16 at 15:44
  • So your `_id` values are strings and sometimes those strings look like ObjectIds? – mu is too short Feb 26 '16 at 19:30
  • _id values are string. – Hare Ram Feb 27 '16 at 04:08
  • Have you tried adding `field :_id, type: String` to your model? Having `_id`s that are strings but actually look like `BSON::ObjectId`s could be a problem though, Mongoid might keep trying to convert them to `ObjectId`s behind your back. – mu is too short Feb 28 '16 at 20:45

1 Answers1

0

Your problem has happened because the document with _id equal to 548210e8d5a81037af06b2d6 was saved before you changed your model saying the _id field should have string type.

I suppose you inserted something like the below code onto your model after inserting that document on DB. Right?

field :_id, type: String, overwrite: true

So... what is happening with document with id 548210e8d5a81037af06b2d6?

Simple. The document was saved with default mongodb _id type - which is ObjectId.

Now (after defining _id type as string) whenever you query DB with 548210e8d5a81037af06b2d6, it will try to find a document with id equal to 548210e8d5a81037af06b2d6 but it won't find because it was stored as ObjectId('548210e8d5a81037af06b2d6').

If you remove field :_id, type: String, overwrite: true from your model, you'll see that the document 548210e8d5a81037af06b2d6 will be found again, whilest zyCMnbTPENeXkhawT won't.

Thus to solve your problem you have to convert every id on mongodb to the same type.

Cristiano Mendonça
  • 1,220
  • 1
  • 10
  • 21