【问题标题】:Return MongoDB/Mongoose Aggregation Match First Letter [duplicate]返回 MongoDB/Mongoose 聚合匹配首字母 [重复]
【发布时间】:2016-05-20 10:38:51
【问题描述】:

我正在尝试在Node.js 中使用MongoosemongoDB 返回一组结果。

我有这样的东西来匹配每个以za 开头的标题,例如:

Model.aggregate(
        { $project: { firstLetter : { $substr : ["$header", 0, 2] }}},
        { $match: { firstLetter : 'za' }},
        { $limit: 40 }
    );

但是当我将结果分配给一个变量时,它只是一个 Aggregate 对象,我无法确定如何处理。

Mongoose 文档状态:

The documents returned are plain javascript objects, not mongoose documents (since any shape of document can be returned).

为什么我没有得到结果?

【问题讨论】:

    标签: mongodb mongoose aggregation-framework nosql


    【解决方案1】:

    事实证明,您需要异步获取数据,这是有道理的(当然)。

    类似:

    Model.aggregate(
            { $project: { firstLetter : { $substr : ["$header", 0, 2] }}},
            { $match: { firstLetter : 'za' }},
            { $limit: 40 }
        ).exec(function(err, data) {
           doSomethingWithData(data);
    });
    

    希望可以帮助别人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-28
      • 2021-07-28
      • 1970-01-01
      • 2014-10-14
      • 2012-08-31
      • 2019-04-15
      • 2022-10-13
      • 1970-01-01
      相关资源
      最近更新 更多