【问题标题】:How to populate fields after mapReduce with Mongoose.js?如何在 mapReduce 之后使用 Mongoose.js 填充字段?
【发布时间】:2013-03-19 08:39:04
【问题描述】:

我想在 mapReduce 之后填充字段。

mongoose.connection.db.collection('createdCollectionNameForResults', function(err, collection) { 
    collection.find({}).populate('ref_field').toArray(function(err, items) { 
           res.send(200, items)
    });
});

但在这里,它给出了错误:

TypeError: Object # has no method 'populate'

因为 collection.find({}) 返回 mongodb 游标。如何填充 ref_field?

【问题讨论】:

  • 您在本机驱动程序集合上调用 find。您需要在 Mongoose 模型上调用 findpopulate 才能工作。
  • 是的,但是我应该怎么做呢?
  • 为您的 mapReduce 创建的集合创建架构和模型,然后像任何其他猫鼬模型一样查询它。见docs

标签: mongodb mapreduce mongoose


【解决方案1】:

考虑到您在 mongoose 中注册了一个名为“createdCollectionNameForResults”的 Schema

var Model = mongoose.model('createdCollectionNameForResults');
Model.find({}).populate('ref_field').exec(function(err, results){
   console.log(err, results); 
});

【讨论】:

    猜你喜欢
    • 2016-03-03
    • 2012-06-14
    • 2014-11-03
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 2014-03-02
    • 2021-12-10
    • 2023-03-18
    相关资源
    最近更新 更多