最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MongoDB: find by one objectId in array - Stack Overflow

programmeradmin4浏览0评论

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' }]
}
);

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' }]
}
);
Share Improve this question edited Apr 16, 2015 at 14:49 Aviram Fireberger asked Apr 16, 2015 at 14:22 Aviram FirebergerAviram Fireberger 4,1688 gold badges51 silver badges70 bronze badges 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? – Aadi Commented Oct 6, 2021 at 8:43
Add a comment  | 

5 Answers 5

Reset to default 8

Convert the id string to ObjectId:

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

GroupSchema.find({'admins': id });

This is ObjectId:

ObjectId("54bd13864ec56c7c12310a79")

and this is string:

trainerId = '54bd13864ec56c7c12310a79'

So maybe you should use ObjectId in your query.

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.

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

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.

发布评论

评论列表(0)

  1. 暂无评论