5

I'm having trouble with query the DB, I want to get all element that the have for example: admin "54bd13864ec56c7c12310a79" in the admins array,

tried it with "$in" but it didn't worked, could it be related to the fact that its an ObjectId?

trainerId = '54bd13864ec56c7c12310a79'
GroupSchema.find({'admins': { $in: [ trainerId ] }}

This is my db:

{
   "_id" : ObjectId("54b93e8e3801ae381e3433be"),
   "groupName" : "Developers Groups",
   "createdBy" : "Ido",
   "creationDate" : "Jan 16 2015",
   "users" : [ 
       ObjectId("54b932c7ac3ec34a85e6246c")
   ],
   "admins" : [ 
       ObjectId("54b932c7ac3ec34a85e6246c"), 
       ObjectId("54bd13864ec56c7c12310a79")
   ],
   "__v" : 0
}

The Schema model is:

module.exports = mongoose.model('Groups' ,
{
    groupName: String,
    createdBy: String,
    creationDate: String,
    admins: [{ type : mongoose.Schema.Types.ObjectId, ref: 'Users' }],
    users: [{ type : mongoose.Schema.Types.ObjectId, ref: 'Users' }]
}
);
1
  • did you solve this answer? I have the exact same issue. I too want to search for object id which is in array. were you able to solve this problem? Commented Oct 6, 2021 at 8:43

5 Answers 5

8

Convert the id string to ObjectId:

var mongoose = require('mongoose'),
    trainerId = '54bd13864ec56c7c12310a79';
var id = mongoose.Types.ObjectId(trainerId);

GroupSchema.find({'admins': id });
Sign up to request clarification or add additional context in comments.

3 Comments

Well you got my in the way to the solution: That was: var trainerId = mongoose.Schema.Types.ObjectId(trainerId); GroupSchema.find({'admins': { $in: [ trainerId ] }} I marked your as the corrent answer thanks :)
@AviramFireberger I'm not sure why you had to use $in, as just using {admins: ObjectId('EXAMPLE')} should have worked, too.
@badteeth I really can't remember why... it was 7 years ago lol
5

This is ObjectId:

ObjectId("54bd13864ec56c7c12310a79")

and this is string:

trainerId = '54bd13864ec56c7c12310a79'

So maybe you should use ObjectId in your query.

Comments

4

If I understand your question correctly, you probably want to use $elemMatch for this. $in should be used when you want to check if a non-array field is equals one of the values specified in the array you pass to $in.

3 Comments

How can I used the $elemMatch in case of ObjectId? I tried : db.groups.find('admins' : { $elemMatch : {'54b932c7ac3ec34a85e6246c'} } ) but it didn't work as well
try { $elemMatch : { $eq: '54b932c7ac3ec34a85e6246c'} }
{ $elemMatch : { $eq: ObjectId("54b932c7ac3ec34a85e6246c")} } should work
2

If i understand your question correctly,you probably can try the $unwind in aggregation,separate the element in admins.

Comments

2

It looks like you want to match the parent document with a subdocument id. You can either use a simple query:

GroupSchema.find({'admins._id': trainerId}})

Or you could use $elemMatch, but as the doc says it's not necessary: If you specify only a single condition in the $elemMatch expression, you do not need to use $elemMatch.

GroupSchema.find({admins: {$elemMatch: {_id: trainerId}}})

As far as casting the string value to an ObjectId, mongoose will automatically do this for you internally.

2 Comments

I dont get an exception in both of them but I do get 0 results. I double check the Id... it is correct. both: ".find({'admins': {$elemMatch: {'_id': '54b932c7ac3ec34a85e6256c'}}})" and ".find({'admins._id': '54b932c7ac3ec34a85e6256c'}})" didn't worked
What is GroupSchema? Convention uses the internal model name as the model object name: Group. That said, it's very odd that you have to cast the string value to an ObjectId if you are using mongoose. Can you post which version of mongoose you are using and what GroupSchema is?

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.