5

For some reason I can't find a document when I search by the id of a nested document. I can perform other finds easily enough so these two work:

User.findOne({"_id" : some_id}, function(err,user){}
User.findOne({"arrayOfNestedDocs.value":someValue}, function(err,user){}

But finding by id of nested doc doesn't work:

User.findOne({"arrayOfNestedDocs._id" : some_id}, function(err,user){}

I can perform the search in a mongo shell so but not via mongoose. Any ideas would be helpful.

henry.oswald
  • 5,304
  • 13
  • 51
  • 73
  • Do your nested documents have an _id attribute and is some_id of type ObjectId? – Matt Jun 19 '11 at 19:12
  • yep they have an _id so I can perform the search in a shell fine (edited question to say that) – henry.oswald Jun 19 '11 at 19:32
  • I guess it can be problem with `some_id` type. It should be mongoId, many peoples faced with same problem in different drivers. They are passing string instead of mongoid for example. Otherwise it sounds like a bug. Also mb you can show us your documents structure? – Andrew Orsich Jun 19 '11 at 19:51
  • I am beginning to think it is a bug, the search will work in older versions of mongoose but not 1.4. Im confident the structure is ok. I guess I was looking to see if there is another way that is now recommended to perform these searches. – henry.oswald Jun 19 '11 at 20:45

2 Answers2

1

I've added it as an issue in the project

onkar
  • 4,427
  • 10
  • 52
  • 89
henry.oswald
  • 5,304
  • 13
  • 51
  • 73
0

If you're trying to find an embedded document then the syntax is:

User.findOne({_id: id}, function(err, user) {
    var embeddedDoc = user.embeddedDocs.id('embeddedDocId');
});
evilcelery
  • 15,941
  • 8
  • 42
  • 54