此问题是关于此堆栈question的。
我正在尝试将用户数据添加到用户类型,然后将其与与贷款类型一起添加的贷款一起添加。我该如何实现?
我有以下数据集。
db={
loans:[
{
loanId : ObjectId("123123123123"),
loanAmount: 1000,
loanType : 1,
userId: 12
} ,{
loanId : ObjectId("123123123123"),
loanAmount: 300,
loanType : 2,
userId: 12
}
],
loanType:[
{_id:ObjectId("12312312"),
loanTypeId : 1,
type : "Home Loan"
},{_id:ObjectId("12312313"),
loanTypeId : 2,
type : "Bike Loan"
}
],
User: [
{
_id: ObjectId("59a504eb6171b554c02292a9"),
"userName": "Shahabaz Shafi",
"dateOfBirth": "1992-01-01",
userId: 12,
userType: 1,
"addres": {
"country": "India",
"state": "Karnataka",
"city": "Bengaluru"
}
}
],
UserType:[
{_id:ObjectId("1233212334"),userTypeId:1, type:'student'},
{_id:ObjectId("1233212334"),userTypeId:1, type:'staff' }
]
}
期望从上述数据集中获得平坦输出
输出:
{
_id: ObjectId("59a504eb6171b554c02292a9"),
"userName": "Shahabaz Shafi",
"dateOfBirth": "1992-01-01",
userId: 12,
userType: 1,
userTypeName : 'student',
"addres": {
"country": "India",
"state": "Karnataka",
"city": "Bengaluru"
}
loans : [
{
loanId : ObjectId("123123123123"),
loanAmount: 1000,
loanType : 1,
type : "Home Loan",
userId: 12,
} ,{
loanId : ObjectId("123123123123"),
loanAmount: 300,
loanType : 2,
type : "Bike Loan",
userId: 12
}
]
}
我尝试了$ lookup,但是它创建了一个单独的数组而不是一个数组。
答案 0 :(得分:1)
/* Collection 1 : users */
{
"_id" : ObjectId("5c46eaf2e166363e18d6ef91"),
"userName" : "Shahabaz Shafi",
"dateOfBirth" : "1992-01-01",
"userId" : 12,
"userType" : 1,
"addres" : {
"country" : "India",
"state" : "Karnataka",
"city" : "Bengaluru"
}
}
/* Collection 2 : userTypes */
/* 1 */
{
"_id" : ObjectId("5c46eb46e166363e18d6ef92"),
"userTypeId" : 1,
"type" : "student"
}
/* 2 */
{
"_id" : ObjectId("5c46eb46e166363e18d6ef93"),
"userTypeId" : 2,
"type" : "staff"
}
/* Collection 3 : loans */
/* 1 */
{
"_id" : ObjectId("5c46eb8be166363e18d6ef94"),
"loanAmount" : 1000,
"loanType" : 1,
"userId" : 12
}
/* 2 */
{
"_id" : ObjectId("5c46eb8be166363e18d6ef95"),
"loanAmount" : 300,
"loanType" : 2,
"userId" : 12
}
/* Collection 4 : loanType */
/* 1 */
{
"_id" : ObjectId("5c46ebc1e166363e18d6ef96"),
"loanTypeId" : 1,
"type" : "Home Loan"
}
/* 2 */
{
"_id" : ObjectId("5c46ebc1e166363e18d6ef97"),
"loanTypeId" : 2,
"type" : "Bike Loan"
}
Solution:
db.users.aggregate([
{
// Join collections users and usertypes
$lookup : {
from: "userTypes",
localField: "userType",
foreignField: "userTypeId",
as: "userTypes"
}
},
{
// Merge keys of array of objects userTypes in existing document
$replaceRoot: { newRoot: { $mergeObjects: [ { $arrayElemAt: [ "$userTypes", 0 ] }, "$$ROOT" ] } }
},
{
// Remove keys userTypes and userTypeId
$project : {
"userTypes": 0,
"userTypeId" : 0
}
},
{
// Join current document obtained with loans collection
$lookup : {
from: "loans",
localField: "userId",
foreignField: "userId",
as: "loans"
}
},
{
// Break array obtained from loans into separate documents
$unwind : "$loans"
},
{
// Join current documents obtained with loanType collection
$lookup : {
from: "loanType",
localField: "loans.loanType",
foreignField: "loanTypeId",
as: "loanTypes"
}
},
{
// Break array obtained from loans into separate documents
$unwind: "$loanTypes"
},
{
// Display fields as per requirement and modify fields accordingly
$project : {
_id:"$_id",
"type" :1,
"userName" : 1,
"dateOfBirth" : 1,
"userId" : 1,
"userType" : 1,
"addres" : 1,
"loanAmount" : "$loans.loanAmount",
"loantype": "$loans.loanType",
"loanTypeName" : "$loanTypes.type",
"loanId" : "$loans._id"
}
},
{
// Merge documents based on condition given and push data in loans array as per output required
$group:{
_id : "$_id",
"type" : {"$first" :"$type" },
"userName" : {"$first" :"$userName" },
"dateOfBirth" : {"$first" :"$dateOfBirth" },
"userId" : {"$first" :"$userId" },
"userType" : {"$first" :"$userType" },
"addres" : {"$first" :"$addres" },
"loans":{
$push:{
"loanAmount" : "$loanAmount",
"loanType" : "$loantype",
"type" : "$loanTypeName",
"loanId" : "$loanId"
}
}
}
}
]);
Output:
/* 1 */
{
"_id" : ObjectId("5c46eaf2e166363e18d6ef91"),
"type" : "student",
"userName" : "Shahabaz Shafi",
"dateOfBirth" : "1992-01-01",
"userId" : 12,
"userType" : 1,
"addres" : {
"country" : "India",
"state" : "Karnataka",
"city" : "Bengaluru"
},
"loans" : [
{
"loanAmount" : 1000,
"loanType" : 1,
"type" : "Home Loan",
"loanId" : ObjectId("5c46eb8be166363e18d6ef94")
},
{
"loanAmount" : 300,
"loanType" : 2,
"type" : "Bike Loan",
"loanId" : ObjectId("5c46eb8be166363e18d6ef95")
}
]
}
@Anthony Winzlet @Shahabaz Please try this.