I need to pull back documents that have ID stored in an array of ObjectIds so I have an "id" (123) and I want all the DOcs where the "tenants" have an array element of (123)
Data looks like this
{
"_id": ObjectId("abc"),
"name": "Miroslav",
"tenants": [
ObjectId("123"),
ObjectId("456")
]
}
{
"_id": ObjectId("abd"),
"name": "Lothar",
"tenants": [
ObjectId("123"),
ObjectId("694")
]
}
of course the mongoDB systax
things.find( { 'tenants': ObjectId(123) } )
works just fine.
Mongoose complains
ReferenceError: ObjectId is not defined
So I tried this
things.find( { 'tenants': mongoose.Schema.ObjectId(123) } )
And in a bazaar twist, mongoose returned ALL records EXCEPT the 2 expected.
I've seen this question posted 3 years ago, and that post didn't have an answer, hopefully someone here will have a solution.
Im using "mongoose": "4.9.8" (due to a specific 'promise' issue I cannot go up a version, at the moment)
thx