英文:
MongoDB : How to multiply a field that appears only in $project?
问题
我正在使用以下方式连接两个集合($lookup):
const LEAD_PRICE = ....; // 不重要
Client.aggregate(
[
{
$lookup: {
from: "clientboughtleads",
localField: "_id",
foreignField: "Client",
as: "ClientLeads"
}
},
{
$project: {
ClientId: "$_id",
RegistrationDate: "$date",
TotalPurchasedLeads: {
$size: "$ClientLeads"
},
IncomeFromClient: { // 这个总是未定义
$multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"]
}
}
}
]
)
我想通过常量和一个已计算的字段TotalPurchasedLeads
的乘积来计算IncomeFromClient
,但它返回了NULL:
[
{
_id: 5e0e43ae82a71e0017dccb20,
ClientId: 5e0e43ae82a71e0017dccb20,
RegistrationDate: 2020-01-02T21:25:34.000Z,
TotalPurchasedLeads: 4,
IncomeFromClient: null // 这个未计算
}
]
在计算一个被$project的字段时,如何获取$lookup集合的大小?
英文:
I'm joining ($lookup) two collection the following way :
const LEAD_PRICE = ....; // doesn't matter
Client.aggregate(
[
{
$lookup: {
from: "clientboughtleads",
localField: "_id",
foreignField: "Client",
as: "ClientLeads"
}
},
{
$project: {
ClientId: "$_id",
RegistrationDate: "$date",
TotalPurchasedLeads: {
$size: "$ClientLeads"
},
IncomeFromClient: { // This one is always undefined
$multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"]
}
}
}
]
I want to calculate IncomeFromClient
by the mul of a const and a field that is been also calculated - TotalPurchasedLeads
, however it returns NULL :
[
[0] {
[0] _id: 5e0e43ae82a71e0017dccb20,
[0] ClientId: 5e0e43ae82a71e0017dccb20,
[0] RegistrationDate: 2020-01-02T21:25:34.000Z,
[0] TotalPurchasedLeads: 4,
[0] IncomeFromClient: null // This one is not calculated
[0] }
[0] ]
How can I get the size of the $lookup collection when calculating a field that is being $projected ?
答案1
得分: 1
请在我们可以在项目中使用关键字"addFields"之后使用"key"。
const LEAD_PRICE = ....; // doesn't matter
Client.aggregate(
[
{
$lookup: {
from: "clientboughtleads",
localField: "_id",
foreignField: "Client",
as: "ClientLeads"
}
},
{
$addFields: {
TotalPurchasedLeads: {
$size: "$ClientLeads"
}
}
},
{
$project: {
ClientId: "$_id",
RegistrationDate: "$date",
TotalPurchasedLeads: 1,
IncomeFromClient: { // This one is always undefined
$multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"]
}
}
}
]
)
英文:
Please use addFields keyword after we can use key in project .
const LEAD_PRICE = ....; // doesn't matter
Client.aggregate(
[
{
$lookup: {
from: "clientboughtleads",
localField: "_id",
foreignField: "Client",
as: "ClientLeads"
}
},
{
$addFields: {
TotalPurchasedLeads: {
$size: "$ClientLeads"
}
}
},
{
$project: {
ClientId: "$_id",
RegistrationDate: "$date",
TotalPurchasedLeads:1,
IncomeFromClient: { // This one is always undefined
$multiply: [LEAD_PRICE / 2, "$TotalPurchasedLeads"]
}
}
}
]
答案2
得分: 1
你不能在相同的 $project
阶段中使用 $project
的值。所以,改为使用其值
{
"$project": {
"ClientId": "$_id",
"RegistrationDate": "$date",
"TotalPurchasedLeads": {
"$size": "$ClientLeads"
},
"IncomeFromClient": {
"$multiply": [LEAD_PRICE / 2, { "$size": "$ClientLeads" }]
}
}
}
英文:
You cannot use the $project
ed value in the same $project
ion stage. So instead use its value
{
"$project": {
"ClientId": "$_id",
"RegistrationDate": "$date",
"TotalPurchasedLeads": {
"$size": "$ClientLeads"
},
"IncomeFromClient": {
"$multiply": [LEAD_PRICE / 2, { "$size": "$ClientLeads" }]
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论