How to return multiple values using Go Mongo Distinct

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

How to return multiple values using Go Mongo Distinct

问题

我想从集合中返回值,其中一个是批次(batch),它应该与特定的过滤器不同,即value="Nice",另一个是vendor。我无法获取vendor的值。

如何使用Distinct实现这一点?我需要使用Find()吗?

  1. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  2. defer cancel()
  3. filter := bson.D{{Key: "batch", Value: ""}}
  4. values, err := db.Collection("xyzcollection").Distinct(ctx, "batch", filter)
  5. if err != nil {
  6. return nil, err
  7. }
  8. batch := make([]string, len(values))
  9. for i, v := range values {
  10. batch[i] = v.(string)
  11. }
  12. fmt.Println(batch)
英文:

I want to return values from collection one is batch which should be distinct with specific filter i.e value="Nice" and another is vendor. I am unable to get vendor values?

How to achieve this using Distinct, Do I have to use Find()?

  1. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  2. defer cancel()
  3. filter := bson.D{{Key: "batch", Value: ""}}
  4. values, err := db.Collection("xyzcollection").Distinct(ctx, "batch", filter)
  5. if err != nil {
  6. return nil, err
  7. }
  8. batch := make([]string, len(values))
  9. for i, v := range values {
  10. batch[i] = v.(string)
  11. }
  12. fmt.Println(batch)

答案1

得分: 1

在Prasad的评论后,我用以下解决方案解决了我的问题。

  1. type Example struct {}
  2. var exm []Example
  3. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  4. defer cancel()
  5. pipeline := []bson.M{
  6. {"$match": bson.M{"status": "Pending"}},
  7. {"$group": bson.M{"_id": "$batch"}},
  8. }
  9. cursor, err := db.Collection("xyzcollection").Aggregate(ctx, pipeline)
  10. if err != nil {
  11. return []Example{}, errors.New(fmt.Sprintf("无法检索数据:%s", err.Error()))
  12. }
  13. var result Example
  14. for cursor.Next(ctx) {
  15. cursor.Decode(&result)
  16. exm = append(exm, result)
  17. }
  18. return exm, nil
英文:

After prasad comment I have solved my problem with this solution.

  1. type Example struct {}
  2. var exm []Example
  3. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  4. defer cancel()
  5. pipeline := []bson.M{
  6. {"$match": bson.M{"status": "Pending"}},
  7. {"$group": bson.M{"_id": "$batch"}},
  8. }
  9. cursor, err := db.Collection("xyzcollection").Aggregate(ctx, pipeline)
  10. if err != nil {
  11. return []Example{}, errors.New(fmt.Sprintf("unable to retrive data: %s ", err.Error()))
  12. }
  13. var result Example
  14. for cursor.Next(ctx) {
  15. cursor.Decode(&result)
  16. exm = append(exm, result)
  17. }
  18. return exm, nil

答案2

得分: -1

我找到了一个类似的问题,

你需要使用db.collection.aggregate查询。请在MongoDB文档中阅读相关内容。

点击这里查看

点击这里查看

英文:

I found a similar question,

You have to use db.collection.aggregate query. Please read it on MongoDB docs

Check this out

Check this out

huangapple
  • 本文由 发表于 2021年10月6日 22:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/69467610.html
匿名

发表评论

匿名网友

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

确定