【发布时间】:2018-06-01 14:49:52
【问题描述】:
我正在从一个集合中提取一组记录并加入另一个集合。
我想在投影字段中添加一个字段,但出现错误 - 代码和错误如下:
db.getCollection("ConnectionEntity").aggregate(
// Pipeline
[
// Stage 1
{
$match: {
"Id":"9c06cb0c-966a-4f6b-b087-816587629079"
}
},
// Stage 2
{
$lookup: // Equality Match
{
from: "Enterprise",
localField: "EnterpriseId",
foreignField: "_id",
as: "joined"
}
},
// Stage 3
{
$project: {
"Enterprise": {"$arrayElemAt": ["$joined.Profile", 0]}
}
},
// Stage 4 - doesn't work
{
$addFields: {
"Enterprise.Id": {"$arrayElemAt": ["$joined._id", 0]}
}
},
]
);
错误:
The following error occurred while attempting to execute the aggregate query
Mongo Server error (MongoCommandException): Command failed with error 28689: '$arrayElemAt's first argument must be an array'.
The full response is:
{
"_t" : "OKMongoResponse",
"ok" : NumberInt(0),
"code" : NumberInt(28689),
"errmsg" : "$arrayElemAt's first argument must be an array",
"$err" : "$arrayElemAt's first argument must be an array"
}
【问题讨论】:
-
ConnectionEntity背后的架构是什么? -
你应该发布你的收藏
标签: mongodb mongodb-query aggregation-framework