Skip to content

Loading…

find by nested document _id no longer works #389

Closed
henryoswald opened this Issue · 6 comments

2 participants

@henryoswald

This used to work in previous versions (1.1.2), I am not quite sure where it stopped working

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

@aheckmann aheckmann added a commit that referenced this issue
@aheckmann aheckmann add test demonstrating #389 working f1924bc
@aheckmann aheckmann closed this
@henryoswald

Thank you, however do you know the syntax for entering an id into a query from a request? The test pulls an Id from an object however if I try and just hard code an _id in it fails.

example which will find nothing:
User.findOne({'arrayOfNestedDocs._id':'4e136e38c7d4b32e70000004'}, function(err,user){..........

I can find by top level _id's and all properties apart from id.

@aheckmann aheckmann reopened this
@aheckmann

Turns out that finding with ObjectIds works but strings doesn't. We should be casting those ids. For now you can

var ObjectId = require('mongoose').Types.ObjectId;
User.findOne({'arrayOfNestedDocs._id': new ObjectId('4e136e38c7d4b32e70000004')}, function(err,user){..........
@henryoswald

thanks, I thought of that as well but express throws an error up when trying to do it:
"This is an abstract interface. Its only purpose is to mark fields as ObjectId in the schema creation."

@aheckmann

are you sure you're using require('mongoose').Types.ObjectId ?

@henryoswald

Ah ha! no I was using require('mongoose').Schema.ObjectId

Thank you very much for the quick responses!

@henryoswald henryoswald closed this
@aheckmann aheckmann reopened this
@aheckmann

reopening until we take care of that casting issue.

@ybogdanov ybogdanov pushed a commit to ybogdanov/mongoose that referenced this issue
@aheckmann aheckmann update #389 test so it fails 7f4a575
@aheckmann aheckmann added a commit that referenced this issue
@aheckmann aheckmann fix test for #389 be57a16
@aheckmann aheckmann closed this in fff744e
@aheckmann aheckmann added a commit that referenced this issue
@aheckmann aheckmann fix test for #389 0f3308b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.