question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using $elemMatch to filter a subdocument array generates error [Error: Can't use $elemMatch with ObjectId.]

See original GitHub issue

Hi, I want to get all the “activities” on the “sites” that are managed by a specific user so, I’ve tried to query activities with the following:

            site: {
                $elemMatch: {
                    managers: { // managers is an array of ObjectId
                        $in: [req.user._id] // this is an ObjectId
                    }
                }
            }

I’m getting the error [Error: Can’t use $elemMatch with ObjectId.] Can’t find much on that error on internet… Thanks!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11

github_iconTop GitHub Comments

3reactions
vkarpov15commented, Mar 31, 2015

If managers is an array of ObjectId, you should just use:

Sites.find({ managers: { $in: [req.user._id] } }, callback);

You could also use the below, but not recommended because $elemMatch is only necessary if you need to match arrays of subdocs by multiple criteria.

m.findOne({
  ids: {
    $elemMatch: {
      $in: [new mongoose.Types.ObjectId('000000000000000000000001')] 
    }
  }
}, function() {});

The query you specified will assume that site is an array which contains subdocs with a “manager” key that’s an _id.

0reactions
vadermemocommented, Oct 29, 2019

one more on the queue of this issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Filtering an array, in a subdocument, in an ... - Stack Overflow
As such, this is the solution I'll go with; namely using #elemMatch to match those element(s) which contain embedded subdocuments and arrays ......
Read more >
Is it possible to use $elemMatch within $filter? - MongoDB
Hi everyone, Is it possible to use elemMatch within filter? This is an example of my data: { "_id" : ObjectId("5e6c26153facb910290f0869"), ...
Read more >
[Solved]-elemMatch search on array of subdocument-mongodb
It seems like you are trying to put work into the "projection" that you should be doing in the query. Rather your statement...
Read more >
multiple filter in mongodb | The AI Search Engine You Control
MongoDB filter operator is used to filter the elements from the array field. We can use multiple condition like $gt, $lt, $gte and...
Read more >
Functional Differences: Amazon DocumentDB and MongoDB
To use a sparse index that you have created in a query, you must use the $exists clause on the fields that cover...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found