【发布时间】:2020-01-14 22:27:33
【问题描述】:
我的 Connection 集合具有名为 authors 的 ObjectId 数组字段。我想连接作者数组中的所有名字和姓氏。现在我从集合中的所有用户那里得到名字。无法使 $match 聚合工作。谢谢!
连接架构
const connectionSchema = new mongoose.Schema({
authors: [{ type: mongoose.Schema.Types.ObjectId, required: true }],
messages: [messageSchema]
});
代码中的问题
const connections = await Connection.aggregate([
{
$lookup: {
from: "users",
let: { users: "users" },
pipeline: [
{ $match: { _id: { $in: ["_id", "$authors"] } } },
{
$project: {
name: {
$concat: ["$firstname", " ", "$lastname"]
}
}
}
],
as: "userName"
}
},
【问题讨论】:
标签: javascript node.js mongodb express mongoose