【发布时间】:2020-10-07 10:12:22
【问题描述】:
我正在使用以下代码接收我的数据集,它运行良好,没有任何错误。
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'* 但是填充函数在上面的代码中运行良好。下面是给我错误的新代码。这里有什么问题?
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
});
【问题讨论】:
-
这能回答你的问题吗? Mongoose populate vs aggregate Mongoose populate 只是 MongoDB 原生聚合阶段
$lookup的包装器,您需要在聚合中使用$lookup阶段,而不是使用$populate,这是聚合中的未知阶段!检查这里如何使用$lookup:: (stackoverflow.com/questions/2350495/…)
标签: node.js mongodb mongoose mongoose-populate