Mongo聚合 – 使用$addFields进行乘法运算

huangapple go评论96阅读模式
英文:

Mongo Aggregate - $addFields with multiplication

问题

我正在使用官方的Golang MongoDB驱动程序,并尝试进行聚合操作。我想根据货币和薪水的乘积对条目进行排序。

import (
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
    //...
    aggregatePipeline := bson.A{}
    aggregatePipeline = append(aggregatePipeline, bson.D{{Key: "$addFields", Value: bson.D{{Key: "trueSalary", Value: bson.D{{"$multiply", bson.A{"salary", "currency"}}}}}}})
    aggregatePipeline = append(aggregatePipeline, bson.D{{"$sort", bson.D{{"trueSalary", -1}}}})
    cursor, _ := myCollection.Aggregate(context.TODO(), aggregatePipeline)
    // cursor 返回 nil。
}

但是,cursor 返回了 nil。

我的 MongoDB 实体中的 "salary" 和 "currency" 都是整数类型。

英文:

I'm using the official mongo driver on golang and trying to aggregate. I want to sort entries based on the multiplication of currency and salary.

import (
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)


func main () {
    //...
    aggregatePipeline := bson.A{}
    aggregatePipeline = append(aggregatePipeline, bson.D{{Key: "$addFields", Value: bson.D{{Key: "trueSalary", Value: bson.D{{"$multiply", bson.A{"salary", "currency"}}}}}}})
    aggregatePipeline = append(aggregatePipeline, bson.D{{"$sort", bson.D{{"trueSalary", -1}}}})
    cursor , _ := myCollection.Aggregate(context.TODO(), aggregatePipeline)
   // cursor returns nil.
}

But cursor returns nil.

My mongo entities all have "salary" and "currency" as Integer.

答案1

得分: 2

在Go语言中,你应该始终检查返回的错误。这将节省你很多时间。

cursor, err := myCollection.Aggregate(context.TODO(), aggregatePipeline)
if err != nil {
    panic(err)
}

这将输出类似以下内容:

panic: (TypeMismatch) Failed to optimize pipeline :: caused by :: $multiply only supports numeric types, not string

$multiply 操作尝试将 salarycurrency字符串 相乘。显然这不是你想要的,你想要的是将 salarycurrency 属性的值相乘,所以在它们前面加上 $ 符号:

aggregatePipeline = append(aggregatePipeline, bson.D{{
    Key: "$addFields",
    Value: bson.D{{
        Key:   "trueSalary",
        Value: bson.D{{"$multiply", bson.A{"$salary", "$currency"}}},
    }},
}})
英文:

In Go you should always check returned errors. That'll save you a lot of time.

cursor, err := myCollection.Aggregate(context.TODO(), aggregatePipeline)
if err != nil {
    panic(err)
}

This will output something like:

> panic: (TypeMismatch) Failed to optimize pipeline :: caused by :: $multiply only supports numeric types, not string

The $multiply operation tries to multiply the salary and currency strings. Obviously this is not what you want, you want to multiply the values of the salary and currency properties, so add a $ sign to them:

aggregatePipeline = append(aggregatePipeline, bson.D{{
	Key: "$addFields",
	Value: bson.D{{
		Key:   "trueSalary",
		Value: bson.D{{"$multiply", bson.A{"$salary", "$currency"}}},
	}},
}})

huangapple
  • 本文由 发表于 2021年11月14日 05:15:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/69958305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定