Let’s say you have boxes with items. Each item may be in more than one box, so every item has an array of ids to boxes that the item lies in.
childA= {…boxIds: [‘000001’, ‘000002’, ‘000003’]…}
In mongodb you can say child.find({boxIds:'000002'}) to get childA and every other children where 000002 is in the array of boxIds.
This is what I tried in mongoose (coffeescript):
data = boxIds: box._id
Child.find data, (err, children) -> doSomething
I always get an empty result though. I can’t find the problem or is this type of query not supported by mongoose?
EDIT
Child = new mongoose.Schema
...
boxIds: Array
...
box._id is the normal mongodb id string
example query in mongodb: db.child.find( {boxIds: '50bb5d10ba1e232401000002'} )
result: ......., "boxIds" : [ "50bb5d10ba1e232401000002" ], .....
EDIT2
Could it be that _id is not a string?
The problem is that
box._idis an ObjectId, but theboxIdsarray field ofChildcontains strings.Try this instead:
The other option would be to define
boxIdsas a string array in the schema and then Mongoose will do the necessary casting for you: