Mongoose/MongoDB:在儿童阵列中加入三个字段

发布于 2025-01-22 09:20:14 字数 4198 浏览 0 评论 0原文

我目前正在尝试创建一个临时字段,该字段将三个字段添加在一起,然后根据用户输入(他们只看到计算并需要匹配的值)查询

我最大的问题是使投射到我需要时工作它。我的字段布局看起来像这样:

{
  _id: ObjectId(),
  userId: "",
  orderNumber: "",
  items: [
           {
             _id: ObjectId(),
             rateInfo: {
                         baseRate: 100,
                         tolls: 50,
                         fees: 25,
                       },
           },
           {
             _id: ObjectId(),
             rateInfo: {
                         baseRate: 200,
                         tolls: 25,
                         fees: 10,
                       },
           },
           {
             _id: ObjectId(),
             rateInfo: {
                         baseRate: 25,
                         tolls: 5,
                         fees: 0,
                       },
           },
         ],
}

我需要能够在rateinfo中添加所有三个字段,并在每个项目数组中显示它们,以使其看起来如此:

{
  _id: ObjectId(),
  userId: "",
  orderNumber: "",
  items: [
           {
             _id: ObjectId(),
             rateInfo: {
                         baseRate: 100,
                         tolls: 50,
                         fees: 25,
                       },
             rateTotal: 175,
           },
           {
             _id: ObjectId(),
             rateInfo: {
                         baseRate: 200,
                         tolls: 25,
                         fees: 10,
                       },
             rateTotal: 235,
           },
           {
             _id: ObjectId(),
             rateInfo: {
                         baseRate: 25,
                         tolls: 5,
                         fees: 0,
                       },
             rateTotal: 30,
           },
         ],
}

我尝试了colling snippets,但似乎都不适合我:

“在此处输入图像描述”

$降低方法从项目数组中总和所有,然后将总数放在订单级别上:

{
  rateTotal: 440
  document: {
    _id: ObjectId(),
    userId: "",
    orderNumber: "",
    items: [
             {
               _id: ObjectId(),
               rateInfo: {
                           baseRate: 100,
                           tolls: 50,
                           fees: 25,
                         },
             },
             {
               _id: ObjectId(),
               rateInfo: {
                           baseRate: 200,
                           tolls: 25,
                           fees: 10,
                         },
             },
             {
               _id: ObjectId(),
               rateInfo: {
                           baseRate: 25,
                           tolls: 5,
                           fees: 0,
                         },
             },
           ],
  }
}

$ Undind方法仅将第一个记录添加到这样:

{
  rateTotal: 175
  document: {
    _id: ObjectId(),
    userId: "",
    orderNumber: "",
    items: [
             {
               _id: ObjectId(),
               rateInfo: {
                           baseRate: 100,
                           tolls: 50,
                           fees: 25,
                         },
             },
             {
               _id: ObjectId(),
               rateInfo: {
                           baseRate: 200,
                           tolls: 25,
                           fees: 10,
                         },
             },
             {
               _id: ObjectId(),
               rateInfo: {
                           baseRate: 25,
                           tolls: 5,
                           fees: 0,
                         },
             },
           ],
  }
}

如果有人可以提供对我做错了什么以及如何纠正的清晰度,这将不胜感激。

注意:由于我需要查询它,因此不可能虚拟。我无法在羊毛架模式内回填计算的字段(使用自定义设置器或Pre Save)。

I am currently trying to create a temporary field that adds three fields together and then query based on a value that a user inputs (they only see the calculated and need it to match)

My biggest problem has been getting the projection to work as I need it. My field layout looks like this:

{
  _id: ObjectId(),
  userId: "",
  orderNumber: "",
  items: [
           {
             _id: ObjectId(),
             rateInfo: {
                         baseRate: 100,
                         tolls: 50,
                         fees: 25,
                       },
           },
           {
             _id: ObjectId(),
             rateInfo: {
                         baseRate: 200,
                         tolls: 25,
                         fees: 10,
                       },
           },
           {
             _id: ObjectId(),
             rateInfo: {
                         baseRate: 25,
                         tolls: 5,
                         fees: 0,
                       },
           },
         ],
}

I need to be able to add all three fields inside rateInfo and display them in each item array to look like so:

{
  _id: ObjectId(),
  userId: "",
  orderNumber: "",
  items: [
           {
             _id: ObjectId(),
             rateInfo: {
                         baseRate: 100,
                         tolls: 50,
                         fees: 25,
                       },
             rateTotal: 175,
           },
           {
             _id: ObjectId(),
             rateInfo: {
                         baseRate: 200,
                         tolls: 25,
                         fees: 10,
                       },
             rateTotal: 235,
           },
           {
             _id: ObjectId(),
             rateInfo: {
                         baseRate: 25,
                         tolls: 5,
                         fees: 0,
                       },
             rateTotal: 30,
           },
         ],
}

I have tried the follow snippets but neither seem to work for me:

enter image description here

enter image description here

The $reduce method sums all from the items array and then put the total at the order level like so:

{
  rateTotal: 440
  document: {
    _id: ObjectId(),
    userId: "",
    orderNumber: "",
    items: [
             {
               _id: ObjectId(),
               rateInfo: {
                           baseRate: 100,
                           tolls: 50,
                           fees: 25,
                         },
             },
             {
               _id: ObjectId(),
               rateInfo: {
                           baseRate: 200,
                           tolls: 25,
                           fees: 10,
                         },
             },
             {
               _id: ObjectId(),
               rateInfo: {
                           baseRate: 25,
                           tolls: 5,
                           fees: 0,
                         },
             },
           ],
  }
}

The $unwind method only adds up the first records like so:

{
  rateTotal: 175
  document: {
    _id: ObjectId(),
    userId: "",
    orderNumber: "",
    items: [
             {
               _id: ObjectId(),
               rateInfo: {
                           baseRate: 100,
                           tolls: 50,
                           fees: 25,
                         },
             },
             {
               _id: ObjectId(),
               rateInfo: {
                           baseRate: 200,
                           tolls: 25,
                           fees: 10,
                         },
             },
             {
               _id: ObjectId(),
               rateInfo: {
                           baseRate: 25,
                           tolls: 5,
                           fees: 0,
                         },
             },
           ],
  }
}

If anyone can provide some clarity on what I'm doing wrong and how to correct, it would be greatly appreciated.

NOTE: mongoose virtuals are not possible since I need to query it. I cannot backfill a calculated field inside the mongoose schema (using a custom setter or pre save).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恰似旧人归 2025-01-29 09:20:14

您可以使用$ MAP将其映射到所需的结构。

db.collection.aggregate([
  {
    "$addFields": {
      "items": {
        "$map": {
          "input": "$items",
          "as": "i",
          "in": {
            _id: "$i._id",
            rateInfo: {
              baseRate: "$i.rateInfo.baseRate",
              tolls: "$i.rateInfo.tolls",
              fees: "$i.rateInfo.fees"
            },
            rateTotal: {
              "$add": [
                "$i.rateInfo.baseRate",
                "$i.rateInfo.tolls",
                "$i.rateInfo.fees"
              ]
            }
          }
        }
      }
    }
  }
])

这是 mongo Playground 供您参考。

对于您的试验,您已经弄清楚了为什么$降低试验。
对于您的$ undind试验,结果实际上是正确的。只是数组被数组“变平”。实际上,您应该能够看到多个文档,因为它们与每个数组条目相对应。当您仅查看第一个文档时,因此您正在查看第一个数组元素的结果。

You can use $map to map to your desired structure.

db.collection.aggregate([
  {
    "$addFields": {
      "items": {
        "$map": {
          "input": "$items",
          "as": "i",
          "in": {
            _id: "$i._id",
            rateInfo: {
              baseRate: "$i.rateInfo.baseRate",
              tolls: "$i.rateInfo.tolls",
              fees: "$i.rateInfo.fees"
            },
            rateTotal: {
              "$add": [
                "$i.rateInfo.baseRate",
                "$i.rateInfo.tolls",
                "$i.rateInfo.fees"
              ]
            }
          }
        }
      }
    }
  }
])

Here is the Mongo playground for your reference.

For your trials, you have figured out why for your $reduce trial.
For your $unwind trial, the result is actually correct. Just the array is "flatten" by the array. You should actually be able to see multiple documents, as they are corresponding to each of the array entries. As you are just viewing the first document, so you are viewing the result of the first array element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文