无法识别的管道阶段名称

无法识别的管道阶段名称

本文介绍了无法识别的管道阶段名称:'$populate'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码来接收我的数据集,它运行良好,没有任何错误.

I was using the following code to receive my data set and it worked fine without any error.

const postslist = await postSchemaModel.find()
        .limit(limit)
        .skip(startIndex)
        .sort({ createdAt: -1 })
        .populate(user_id)
        .populate("user_id", "img user_name _id");

但是,就我而言,我想使用 $geonear 获取结果集,并且我使用了以下代码.它给了我这个错误 * Unrecognized pipeline stage name: '$populate'*但是 populate 函数在上面的代码中运行良好.下面一个是给我错误的新代码.这里有什么问题?

But , In my case I want to get the result set with $geonear and I used the following code. There it gives me this error * Unrecognized pipeline stage name: '$populate'*But the populate function was working fine with the above code. Below one is the new code that gives me the error. What can be the fault here ?

postSchemaModel.aggregate([{
        "$geoNear": {
            "near": { "type": "Point", "coordinates": [6.7336665, 79.8994071], "Typology": "post" },
            "distanceField": "dist.calculated",
            "maxDistance": 5000,
            "includeLocs": "dist.location",
            "spherical": true
        }
    },
    { "limit": limit },
    { "skip": startIndex },
    { "$sort": { "createdAt": -1 } },
    { "populate": user_id },
    { "populate": "'user_id', 'img user_name _id'" }

]).then(async function(posts) {
    //some code here
});

推荐答案

您的非工作代码使用聚合管道.

Your non-working code uses an aggregation pipeline.

填充文档 没有提及任何关于聚合的内容.

populate documentation does not mention anything about aggregation.

看起来您试图将 populate 函数转换为聚合管道语法,但聚合管道由 MongoDB 服务器执行而不由 Mongoose 解释,并且服务器不知道populate"是什么,因此只是不工作.

It looks like you attempted to translate the populate function into aggregation pipeline syntax, but the aggregation pipeline is executed by MongoDB server and not interpreted by Mongoose and the server has no idea what "populate" is, hence that just doesn't work.

这篇关于无法识别的管道阶段名称:'$populate'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:56