I have a $addfields method. I need to get the assignedTo value of $addfields to next $lookup method.
I need to add some aggregation to get the cgPrice value. using $addfields but cgPrice not added in the response.
Existing Code:
let aggregate = aggregateInit ? aggregateInit : DatasetModel.aggregate();
aggregate = aggregate.addFields({
state: getHistoryDateEntry("state", {}, "max"),
lastFileId: getHistoryDateEntry(
"fileId",
{
$ne: [{ $type: "$$item.fileId" }, "missing"],
},
"max"
),
assignedTo: getHistoryDateEntry(
"assignedTo",
{
$and: [
{ $ne: [{ $type: "$$item.assignedTo" }, "missing"] },
{
$eq: ["$$item.state", State.AssignedToPost],
},
],
},
"max"
),
lastChange: getHistoryDateEntry("date", {}, "max"),
firstAssigned: getHistoryDateEntry(
"date",
{
$eq: ["$$item.state", State.AssignedToPost],
},
"min"
),
firstBillable: getHistoryDateEntry(
"date",
{
$in: [
"$$item.state",
[
State.Ready,
State.Error,
State.NotSymmetricalError,
State.AnnotationError,
State.FixAnnotationError,
State.VariantPositioningError,
],
],
},
"min"
),
lastMeta: getHistoryDateEntry("meta", {}, "max"),
lens: getHistoryDateEntry(
"lens",
{
$eq: ["$$item.state", State.UpdateLens],
},
"max"
),
lastStorageInfo: getHistoryDateEntry(
"storageInfo",
{
$eq: ["$$item.state", State.UpdateStorageInfo],
},
"max"
),
scanPositions: getHistoryDateEntry(
"scanPositions",
{
$and: [
{ $ne: [{ $type: "$$item.scanPositions" }, "missing"] },
{
$in: ["$$item.state", [State.Rescan, State.UpdateScanPositions]],
},
],
},
"max"
),
importWorkflow: {
$cond: {
if: {
$ne: [
{
$type: getHistoryDateEntry(
"date",
{
$eq: ["$$item.state", State.Importing],
},
"max"
),
},
"missing",
],
},
then: true,
else: false,
},
},
thumbnailUrl: getHistoryDateEntry(
"fileId",
{
$eq: ["$$item.state", State.FinalQa],
},
"max"
),
});
my code:
aggregate = aggregate..lookup({
from: "users",
localField: "assignedTo",
foreignField: "id",
as: "matchedUsers",
})
.addFields({
cgPrice: {
$getField: {
field: "price",
input: {
$first: {
$filter: {
input: {
$getField: {
field: "prices",
input: {
$first: "$matchedUsers",
},
},
},
as: "currentPrice",
cond: {
$and: [
{
$gte: ["$firstBillable", "$$currentPrice.beginDate"],
},
{
$lt: ["$firstBillable", "$$currentPrice.endDate"],
},
],
},
},
},
},
},
},
})
.project({
matchedUsers: 0,
})
but $lookup only support the string value. how to pass the assignedTo inside $lookup
Current Response data:
{
"state": "scanning",
"lastFileId": "b4bfbab3-1f6f-4b83-bb15-f7bb8b1f1a0d",
"assignedTo": "57xf16433c0d3ffe0ac084a4",
"lastChange": "2020-05-05T07:29:01.639Z",
"lastMeta": "Remark",
"lens": {
"coatingLabel": "Silver",
"tintLabel": "Solid Grey 75%"
},
"lastStorageInfo": {
"comment": "A15",
"checkedOut": false
},
"scanPositions": 8,
"checkedOut": false,
"importWorkflow": false,
"firstAssigned": "2020-05-05T07:29:01.639Z",
"firstBillable": "2020-05-05T07:29:01.639Z",
"thumbnailUrl": "https://www.url.com/test.png"
}
Excepted Response Data:
{
"state": "scanning",
"lastFileId": "b4bfbab3-1f6f-4b83-bb15-f7bb8b1f1a0d",
"assignedTo": "57xf16433c0d3ffe0ac084a4",
"lastChange": "2020-05-05T07:29:01.639Z",
"lastMeta": "Remark",
"lens": {
"coatingLabel": "Silver",
"tintLabel": "Solid Grey 75%"
},
"lastStorageInfo": {
"comment": "A15",
"checkedOut": false
},
"scanPositions": 8,
"checkedOut": false,
"importWorkflow": false,
"firstAssigned": "2020-05-05T07:29:01.639Z",
"firstBillable": "2020-05-05T07:29:01.639Z",
"cgPrice": 18.50,
"thumbnailUrl": "https://www.url.com/test.png"
}
I want add cgPrice to current response.