1

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

3
  • can you show the tennants part of the schema ? Commented Jan 29, 2018 at 10:42
  • The tenants are the users - parent/child relationship...the tenant IDs refer to the _id of a different document. Commented Jan 29, 2018 at 10:54
  • try this: things.find( { tenants: mongoose.Types.ObjectId("123") } ) Commented Jan 29, 2018 at 11:32

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

adequate enough for me to know what Im reading as I go digging. Thank you.
0

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.

1 Comment

give me a second to find the docs again, then I'll post a full answer
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.