如何在javascript对象数组中查找或聚合对象字段(在mongodb中)?
我希望在我的mongoose模式中有一个字段contractType,它是一个对象数组,每个对象的属性authorizedBy进入第二级 contractType字段可能有许多项,我想通过在另一个名为admins的集合中查找它来了解每个授权项(我也将在下面介绍) 这是我的管理员收藏如何在javascript对象数组中查找或聚合对象字段(在mongodb中)?,mongodb,lookup,aggregation,mongoose-schema,unwind-segue,Mongodb,Lookup,Aggregation,Mongoose Schema,Unwind Segue,我希望在我的mongoose模式中有一个字段contractType,它是一个对象数组,每个对象的属性authorizedBy进入第二级 contractType字段可能有许多项,我想通过在另一个名为admins的集合中查找它来了解每个授权项(我也将在下面介绍) 这是我的管理员收藏 const adminSchema = new Schema( { firstName: String, lastName: String, role: String }, {
const adminSchema = new Schema(
{
firstName: String,
lastName: String,
role: String
},
{
timestamps: true,
versionKey: false,
},
)
我在专业模式mongodb compass中有这个:
{
"_id": ObjectId("6009a0d0874f0900086ee0ce"),
"firstName": "xxxx",
"gender": "NOT_SPECIFIED",
"contractType": [
{
"authorizedBy": ObjectId("5fad90665d963cbbbd4a6580"),
"document": "document 1"
},
{
"authorizedBy": ObjectId("5fad90665d963cbbbd4a6580"),
"document": "document 2"
}
]
}
这是管理员集合的管理员(adminSchema):
我会得到下面这样的响应,另外,我希望获得对象的所有字段(大约20个),而不是手动添加每个字段,就像javascript中的扩展运算符(…项)
{
"_id": ObjectId("6009a0d0874f0900086ee0ce"),
"firstName": "xxxx",
"contractType": [
{
"authorizedBy": {
"_id": "5fad90665d963cbbbd4a6580",
"firstName": "firstname",
"lastName": "lastname",
"role": "SAM"
},
"document": "document 1"
},
{
"authorizedBy": {
"_id": "5fad90665d963cbbbd4a6580",
"firstName": "firstname",
"lastName": "lastname",
"role": "SAM"
},
"document": "document 2"
}
]
}
我在mongo compass中试过这个
[
{
'$match': {
'_id': new ObjectId('6009a0d0874f0900086ee0ce')
}
}, {
'$unwind': {
'path': '$contractType'
}
}, {
'$lookup': {
'from': 'admins',
'localField': 'contractType.authorizedBy',
'foreignField': '_id',
'as': 'contractType.authorizedBy'
}
}, {
'$unwind': {
'path': '$contractType.authorizedBy'
}
}, {
$group': {
'_id': '$_id'
}
}
]
但我不知道如何将其余元素(大约20个)放在同一个对象中:(您所做的几乎是正确的
db.professionalSchema.aggregate([
{"$match": {"_id": ObjectId("6009a0d0874f0900086ee0ce") } },
{"$unwind": {"path": "$contractType" }},
{
"$lookup": {
"from": "adminSchema",
"localField": "contractType.authorizedBy",
"foreignField": "_id",
"as": "contractType.authorizedBy"
}
},
{
"$addFields": {
"contractType.authorizedBy": {
$ifNull: [
{ $arrayElemAt: [ "$contractType.authorizedBy", 0 ] },
""
]
}
}
},
{
"$group": {
"_id": "$_id",
"firstName": { $first: "$firstName" },
"gender": { $first: "$gender"},
contractType: { $push: "$contractType" }
}
}
])
工作你能分享示例输入和预期输出吗?我用这些信息更新问题:但是如果在我的专业模式中,我有20多个字段,其中一些字段嵌入了文档…有没有办法将所有属性传递给组的结果?比如javascript中的spread操作符{…items}
[
{
'$match': {
'_id': new ObjectId('6009a0d0874f0900086ee0ce')
}
}, {
'$unwind': {
'path': '$contractType'
}
}, {
'$lookup': {
'from': 'admins',
'localField': 'contractType.authorizedBy',
'foreignField': '_id',
'as': 'contractType.authorizedBy'
}
}, {
'$unwind': {
'path': '$contractType.authorizedBy'
}
}, {
$group': {
'_id': '$_id'
}
}
]
db.professionalSchema.aggregate([
{"$match": {"_id": ObjectId("6009a0d0874f0900086ee0ce") } },
{"$unwind": {"path": "$contractType" }},
{
"$lookup": {
"from": "adminSchema",
"localField": "contractType.authorizedBy",
"foreignField": "_id",
"as": "contractType.authorizedBy"
}
},
{
"$addFields": {
"contractType.authorizedBy": {
$ifNull: [
{ $arrayElemAt: [ "$contractType.authorizedBy", 0 ] },
""
]
}
}
},
{
"$group": {
"_id": "$_id",
"firstName": { $first: "$firstName" },
"gender": { $first: "$gender"},
contractType: { $push: "$contractType" }
}
}
])