我想传入一个ObjectId数组以匹配在数组中具有这些ID的集合
模型如下:
const MyThing = new Schema({
my_ids: [{
type: Schema.Types.ObjectId
,ref: 'Things'
}]
,desc: {
type: String
}
});
const myThing = module.exports = mongoose.model('myThing', MyThing);
我的路由器看起来像:
let arrIds = ['123456789101112','123456789101113'];
myThing.getTheStuff( arrIds, (err, stuff) => {
res.json(stuff);
});
我的模型如下:
module.exports.getTheStuff = function( arrIds, callback, limit) {
const query = { 'my_ids': { $all: arrIds } };
console.log( 'query', query );
MessageThread.find( query )
.exec( callback );
};
上面的console.log()产生了这个结果:
{ my_ids: { '$all': [ '5b579074dc1eac0014276442', '5a9ccf7de6348936d88b36ac' ] } }
我觉得我应该做这样的事情:
const query = { 'my_ids': { $all: mongoose.Types.ObjectId(arrIds) } };
或
const query = { 'my_ids': { $all: mongoose.Types.Array.ObjectId(arrIds) } };