j-p
j-p

Reputation: 3818

mongooseJS find docs with IDs in an array

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

Upvotes: 1

Views: 6753

Answers (3)

masterchinchilla
masterchinchilla

Reputation: 11

I was having trouble with this, that is, finding documents using mongoose where I needed to filter by a property array containing a specific ObjectId.

For example, to find "meal" documents per mealType where:

const someMealDocument = {
    _id: abc123
    mealType: [ someObjectId1, someObjectId2 ]
}

I am using mongoose 9.5.1. The above answers seem to not work with my later version of mongoose.

What eventually worked for me was this syntax:

let thisMealTypeId="someString";
MealModel.find({
     mealType: thisMealTypeId
})

Just that, no "mongoose.Types.ObjectId," no "$in," no other complexity.

I couldn't find this answer anywhere so I figured it out by long trial and error. Hopefully posting this here will save someone else from having to do that.

Upvotes: 0

Rakan
Rakan

Reputation: 400

to convert to ObjectId you need to use:

things.find({tennants: mongoose.Types.ObjectId("123")});

the difference between mongoose.Schema.ObjectId and mongoose.Types.ObjectId is that the latter is the ObjectId constructor function. it can even be used like:

var id = new mongoose.Types.ObjectId("123");

to create an objectId and store it in the id variable.

while mongoose.Schema.ObjectId (or mongoose.Schema.Types.ObjectId) refers to a data type. that's what you'd use in your schema. the same way you might set

name: String

in a schema, you use mongoose.Schema.Types.ObjectId to specify that the data type is a mongoose ObjectId.

note: I remember reading this in the docs a few months ago, but i was unable to find the docs now, i think my explanation is fairly adequate though

Upvotes: 5

j-p
j-p

Reputation: 3818

rakan316 wrote -

try this: things.find( { tenants: mongoose.Types.ObjectId("123") } )

And that worked...

rakan316, if you post this as an answer, I'll make it as correct, and up vote it. Thx, I'd like to know WHY, if you know.

Upvotes: 0

Related Questions