Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.92k stars 3.84k forks source link

find object ids in document with array #3090

Closed linus-amg closed 9 years ago

linus-amg commented 9 years ago

hi, great job you are doing with mongoose, thank you very much!

we have one problem though: we can't find documents which have a specific ObjectId in a array of these documents, here I will put an example:

2 example documents of users:

{
  "_id": ObjectId("abc"),
  "name": "Miroslav",
  "tenants": [
    ObjectId("123"),
    ObjectId("456")
  ]
}

{
  "_id": ObjectId("abd"),
  "name": "Lothar",
  "tenants": [
    ObjectId("123"),
    ObjectId("694")
  ]
}

working mongodb query:

db.users.find({ "tenants": new ObjectId("123") });

failing mongoose queries (not failing.. but returning an empty array..):

Users.find({ "tenants": mongoose.Types.ObjectId("123") });
Users.find({ "tenants": new ObjectId("123") });

We also tried aggregates, unwinding the tenants array and using match, elementMatch whatsoever, the same agregates with the native mongodb driver return the documents which have the Item ObjectId("123"), so i thought it should have something to do with mongoose then.

Having debugging enabled i saw surprisingly that in effect mongoose sends the same query which i am executing in robomongo for example, mongoose does not return anything but the mongo-cli or robomongo with the same query do return what i would expect.

Mongoose: users.find({ tenants: ObjectId("5586c3cba0d822b7341fc0d4") }) { fields: undefined }  

the tenants type inside the user schema is [{ type: mongoose.Schema.Types.ObjectId, ref: 'tenant' }]

im using mongoose 4.0.5

what could i do to make it work?

linus-amg commented 9 years ago

if i write the following aggregate:

MasterUser.aggregate [
      $unwind: '$tenants'
    ,
      $match:
        'tenants': new ObjectId(tenantId)
  ], (err, results) ->
    callback err, results

the mongoose debugging does not cast the ObjectId right:

Mongoose: users.aggregate([ { '$unwind': '$tenants' }, { '$match': { tenants: 5586c579936847d236fbdc03 } } ]) {} 

the same thing happens if i use the mongoose ObjectId Type:

mongoose.Types.ObjectId(tenantId)
linus-amg commented 9 years ago

Fun fact i forgot to mention: if i change the arrays item type to string, and search the string, everything works as expected, its only if its of type ObjectId that it doesnt work.

linus-amg commented 9 years ago

neither does findOne work

Mongoose: users.findOne({ tenants: ObjectId("5580b0695b03e2e592bf722e") }) { fields: undefined }  

it works though if the document which im searching is having a string with that id and if im searching for the string instead of using ObjectId()

linus-amg commented 9 years ago

just found out that it works with version 3.8.31 :)

vkarpov15 commented 9 years ago

This looks related to #3079, but I haven't been able to reproduce this in any case. Can you send me the following:

  1. Mongo shell output that contains one user for which this is happening, exactly.
  2. Your complete mongoose schema
  3. Output of npm list | grep "mongo"

so I can try to debug a little more?

vkarpov15 commented 9 years ago

No response for a while. Re-open if this is still an issue.

Davidrl1000 commented 8 years ago

I am experiencing this issue, did you find any solution to this?

vkarpov15 commented 8 years ago

@Davidrl1000 please provide repro instructions / code samples.

shubham-rathod1 commented 3 years ago

experienced the same issue does anyone have fix for this?

shubham-rathod1 commented 3 years ago

experienced the same issue does anyone have fix for this? I found I had this issue because of my import. I was importing a JSON file using MongoDB compass.