我正在尝试使用Mongoose和Node.js在MongoDB中使用填充操作进行groupBy。
我有一个单独的集合,其中包含client_id
和project_id
。一个客户可以有很多项目,这就是为什么我创建了另一个集合以便更好地理解的原因:
var CustomerHasProjectSchema = new mongoose.Schema({
client_id: {type: mongoose.Schema.Types.ObjectId, ref: 'Customer'},
project_id: {type: mongoose.Schema.Types.ObjectId, ref: 'Project'},
txn_date: {
type: Date,
default: Date.now
},
status: {
type: Number,
default : 1
}
});
我正在尝试做类似
的事情GROUPBY(CLIENT_ID) 并填充(user_id)
我尝试的确切查询是
return customerHasProject.aggregate(
[
{
$match: {
$and: [
{ client_id: id }
]
}
},
{
$group : {
_id : "$client_id",
count: {
$sum: 1
}
}
},
{
$lookup: {
from: "project",
localField: "project_id",
foreignField: "_id",
as: "projects"
}
},
{
$unwind : {
"path" : "$project_id"
}
}
]
我在另一个StackOverflow post的帮助下尝试了这个。
我也尝试过:
return customer2.default.find({'_id' : req.params.id}).exec().then(response=>{
project2.default.find({'client_id' : response[0]._id}).populate('client_id').then(customerWithProject=>{
res.json(customerWithProject)
})
})
但是这会在每个项目中返回客户端详细信息,结果如下:
[
{
_id: "5883b189872c7d00042e1630",
projectTitle: "Marketing Analytics",
client_id: {
_id: "58833d9e1079ba000445a3a1",
clientName: "Anil",
companyName: "fgjhtrjhed",
website: "jhtryjh",
clientEmail: "frhyrt@tyhtr.dfkgjoiu",
mobileNumber: 549821654,
companyLocation: "New Delhi, Delhi, India",
clientCategory: "Freelancer",
__v: 0,
status: 1,
txn_date: "2017-01-21T10:53:18.360Z"
},
start_date: "2017-01-21T18:30:00.000Z",
end_date: "2017-01-30T18:30:00.000Z",
domain: "Back to the Future",
proposed_amount: 138000,
expectedFinalAmount: 200000,
__v: 0,
status: 1,
txn_date: "2017-01-21T19:07:53.214Z",
keywords: [
"JavaScript",
"AngularJs",
"C"
]
},
{
_id: "5883b213872c7d00042e1631",
projectTitle: "Indus Milk",
client_id: {
_id: "58833d9e1079ba000445a3a1",
clientName: "Anil",
companyName: "fgjhtrjhed",
website: "jhtryjh",
clientEmail: "frhyrt@tyhtr.dfkgjoiu",
mobileNumber: 549821654,
companyLocation: "New Delhi, Delhi, India",
clientCategory: "Freelancer",
__v: 0,
status: 1,
txn_date: "2017-01-21T10:53:18.360Z"
},
domain: "Back to the Future",
start_date: "2017-01-01T18:30:00.000Z",
end_date: "2017-01-06T18:30:00.000Z",
proposed_amount: 9200,
expectedFinalAmount: 15000,
__v: 0,
status: 1,
txn_date: "2017-01-21T19:10:11.403Z",
keywords: [
"Java",
"JavaScript"
]
},
{
_id: "5883b817872c7d00042e1632",
proposed_amount: 120000,
start_date: "2017-01-09T18:30:00.000Z",
end_date: "2017-02-03T18:30:00.000Z",
domain: "Back to the Future",
client_id: {
_id: "58833d9e1079ba000445a3a1",
clientName: "Anil",
companyName: "fgjhtrjhed",
website: "jhtryjh",
clientEmail: "frhyrt@tyhtr.dfkgjoiu",
mobileNumber: 549821654,
companyLocation: "New Delhi, Delhi, India",
clientCategory: "Freelancer",
__v: 0,
status: 1,
txn_date: "2017-01-21T10:53:18.360Z"
},
projectTitle: "Something",
__v: 0,
status: 1,
txn_date: "2017-01-21T19:35:51.835Z",
keywords: [
"C++",
"C"
]
}
]
期待像
这样的东西[
{
client_id: {
_id: "58833d9e1079ba000445a3a1",
clientName: "Anil",
companyName: "fgjhtrjhed",
website: "jhtryjh",
clientEmail: "frhyrt@tyhtr.dfkgjoiu",
mobileNumber: 549821654,
companyLocation: "New Delhi, Delhi, India",
clientCategory: "Freelancer",
__v: 0,
status: 1,
txn_date: "2017-01-21T10:53:18.360Z"
},
project_id : [
{
_id: "5883b213872c7d00042e1631",
projectTitle: "Indus Milk",
domain: "Back to the Future",
start_date: "2017-01-01T18:30:00.000Z",
end_date: "2017-01-06T18:30:00.000Z",
proposed_amount: 9200,
expectedFinalAmount: 15000,
},{
_id: "5883b817872c7d00042e1632",
proposed_amount: 120000,
start_date: "2017-01-09T18:30:00.000Z",
end_date: "2017-02-03T18:30:00.000Z",
domain: "Back to the Future",
projectTitle: "Something",
expectedFinalAmount: 15000,
}
]
}
]