Collection.find({_ id:{$ in:myArray}}似乎不起作用

时间:2013-05-24 07:52:44

标签: mongodb meteor

我觉得这是一个ditz,因为我已经使用了Mongo几年了,这似乎应该是一个简单而简单的查询。但我似乎无法为我的生活查询一系列记录。这是基本代码:

所以,我一直在创建一个文档ID集合,如下所示:

\\first, I create a collection of document ids  
Rooms.update(Session.get('selected_room'), {$addToSet: {reservations: this._id}})

\\then, I pull the array of ids like so
var reservationsList = Rooms.findOne(selectedRoomId).reservations;

\\ when I inspect, everything is fine and it produces an array of document ids 
console.log(JSON.stringify(reservationList)

["Rym8yc4GbNmnNcsmx","msLcBMRQxBdjgLddE","XG2z4qQRD5j4ERBca"]

所以,到目前为止,一切正常。这是我开始遇到问题的地方:

//doesn't work
var cursor = Schedule.find({'_id': {$in: reservationsList }});

//doesn't work
var cursor = Schedule.find({_id: {$in: reservationsList }});

//doesn't work
var cursor = Schedule.find({_id: reservationsList });

//doesn't work
var cursor = Schedule.find(reservationsList);

所有这些都会导致TypeError {}。

有关如何进行此查询的任何想法?我想要做的就是将一个_ids数组传递给我的find()函数,然后获取文档的光标。

编辑:以下 工作,所以我知道它不是MongoId包装器等的问题:

record = Schedule.findOne({'_id': reservationsList[0]});

1 个答案:

答案 0 :(得分:1)

呸。写下这个问题让我整理好自己的想法,一旦完成并发布问题,我就把它弄清楚了。我忘记了.fetch():

Schedule.find({_id: {$in: reservationsList }}).fetch();
相关问题