0

I have a mongoose schema of RoomSchema like below

{
  .....
  venue_details : {type: Schema.ObjectId, ref: 'VenueDetail'}
  .....
}

When I'm searching with one of the ObjectIds that I am having with me in mongodb system console it is giving correct result for below query.

db.rooms.find({venue_details: "576a6aa52edadbba50ff0944"})

But whne I'm searching inside the mongoose. I'm getting an empty array.

Room.find({venue_details: new ObjectId("576a69c32edadbba50ff093e")},function(err, docs){
  if(err){
    console.log(err);
  }else{
  console.log(docs);}
})

I've tried putting ObjectId = mongoose.Types.ObjectId , also ObjectId = mongoose.Schema.ObjectId, and also ObjectId = mongoose.Schema.Types.ObjectId

Even tried just putting String in the place of new ObjectId("576a69c32edadbba50ff093e") just like "576a69c32edadbba50ff093e". Even it is not working.

I'm using mongoose V 4.4.1, and MongoDB V 3.2.7

Akhil P
  • 1,580
  • 2
  • 20
  • 29
  • Did you initially create or populate the database with another schema (where `venue_details` was of type `String`)? – robertklep Jun 24 '16 at 11:08
  • I used populate for the same Room Schema. Populate is working fine. But I don't know why this is not working! – Akhil P Jun 24 '16 at 11:12
  • It looks like the `venue_details` in the database is of type `String`, which is unexpected with the schema that you're showing. Because of the schema, Mongoose will cast query properties to `ObjectId`, which is why it may not work. However, question is: why is it a string in your database? – robertklep Jun 24 '16 at 11:17
  • I got it. I have inserted into db manually. How can I convert it into ObjectId() – Akhil P Jun 24 '16 at 11:29
  • Check out [this answer](http://stackoverflow.com/a/10289547/893780), specifically the `db.foo.find().forEach(...)` command used there. – robertklep Jun 24 '16 at 12:12
  • it solved my problem. @robertklep thank you.! – Akhil P Jun 30 '16 at 10:51

0 Answers0