Mongoose/MongoDB:在儿童阵列中加入三个字段
我目前正在尝试创建一个临时字段,该字段将三个字段添加在一起,然后根据用户输入(他们只看到计算并需要匹配的值)查询
我最大的问题是使投射到我需要时工作它。我的字段布局看起来像这样:
{
_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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
$ MAP
将其映射到所需的结构。这是 mongo Playground 供您参考。
对于您的试验,您已经弄清楚了为什么
$降低
试验。对于您的
$ undind
试验,结果实际上是正确的。只是数组被数组“变平”。实际上,您应该能够看到多个文档,因为它们与每个数组条目相对应。当您仅查看第一个文档时,因此您正在查看第一个数组元素的结果。You can use
$map
to map to your desired structure.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.