如何在MongoDB的Golang中找到一个字段的最大值?

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

How to find maximum value of a field in mongodb golang?

问题

这是我的代码。即使有更多的块(例如3000个块),我总是得到最大值999。

这是文档的样子。

如何在MongoDB的Golang中找到一个字段的最大值?

  1. func GetLatestBlockFromMongoDB() int{
  2. if contains(ReturnBlocksExists(), "blocks") == true {
  3. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  4. var blockheights []models.Block
  5. defer cancel()
  6. options := options.Find().SetProjection(bson.M{"block.header.blockNumber": 1})
  7. options.SetSort(bson.D{{"block.header.blockNumber", -1}})
  8. options.SetLimit(1)
  9. results, err := blocksCollections.Find(context.TODO(), bson.D{}, options)
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. //以最优方式从数据库中读取
  14. defer results.Close(ctx)
  15. for results.Next(ctx) {
  16. var singleBlock models.Block
  17. if err = results.Decode(&singleBlock); err != nil {
  18. fmt.Println(err)
  19. }
  20. blockheights = append(blockheights, singleBlock)
  21. }
  22. fmt.Println("%v", blockheights[0].Block.Header.BlockNumber)
  23. if len(strings.TrimSpace(blockheights[0].Block.Header.BlockNumber)) > 0{
  24. i, err := strconv.Atoi(blockheights[0].Block.Header.BlockNumber)
  25. if err != nil {
  26. glog.Fatalf("%v", err)
  27. }
  28. return i
  29. } else {
  30. return 0
  31. }
  32. } else {
  33. return 0
  34. }
  35. }

我如何获得blockNumber的最大值?我认为问题可能是blockNumber是一个字符串而不是整数。我不确定。我已经设置了排序和限制为1,所以它应该正常工作。

英文:

This is my code. I always get maximum value of 999, even when there are more blocks (e.g 3000 blocks).

This is what the document(s) look like.

如何在MongoDB的Golang中找到一个字段的最大值?

  1. func GetLatestBlockFromMongoDB() int{
  2. if contains(ReturnBlocksExists(), "blocks") == true {
  3. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  4. var blockheights []models.Block
  5. defer cancel()
  6. options := options.Find().SetProjection(bson.M{"block.header.blockNumber": 1})
  7. options.SetSort(bson.D{{"block.header.blockNumber", -1}})
  8. options.SetLimit(1)
  9. results, err := blocksCollections.Find(context.TODO(), bson.D{}, options)
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. //reading from the db in an optimal way
  14. defer results.Close(ctx)
  15. for results.Next(ctx) {
  16. var singleBlock models.Block
  17. if err = results.Decode(&singleBlock); err != nil {
  18. fmt.Println(err)
  19. }
  20. blockheights = append(blockheights, singleBlock)
  21. }
  22. fmt.Println("%v", blockheights[0].Block.Header.BlockNumber)
  23. if len(strings.TrimSpace(blockheights[0].Block.Header.BlockNumber)) > 0{
  24. i, err := strconv.Atoi(blockheights[0].Block.Header.BlockNumber)
  25. if err != nil {
  26. glog.Fatalf("%v", err)
  27. }
  28. return i
  29. } else {
  30. return 0
  31. }
  32. } else {
  33. return 0
  34. }
  35. }

How can i get the maximum value of blockNumber? I think the problem might be because blockNumber is a string but not an integer. I'm not sure. I did set sort and also limit 1 so it should normally work.

答案1

得分: 2

是的,你说得对,从图像中我可以看到blockNumber字段是一个字符串,所以你不是在比较整数,而是在比较字符串,其中"999"大于"3000":

例如:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func findMax(a []string) string {
  6. m := a[0]
  7. for _, value := range a {
  8. if value > m {
  9. m = value
  10. }
  11. }
  12. return m
  13. }
  14. func main() {
  15. blocks := []string{"999", "2000", "3000"}
  16. ma := findMax(blocks)
  17. fmt.Println(ma)
  18. }
  1. $ go run .
  2. 999

还可以查看这个问题

英文:

Yes, you are right, from the image, I can see that the blockNumber field is a string, so you are not comparing integers, you are comparing string, where "999" is greater than "3000":

For example:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func findMax(a []string) string {
  6. m := a[0]
  7. for _, value := range a {
  8. if value > m {
  9. m = value
  10. }
  11. }
  12. return m
  13. }
  14. func main() {
  15. blocks := []string{"999", "2000", "3000"}
  16. ma := findMax(blocks)
  17. fmt.Println(ma)
  18. }
  1. $ go run .
  2. 999

Check out this question too.

huangapple
  • 本文由 发表于 2022年7月2日 03:26:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/72833835.html
匿名

发表评论

匿名网友

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

确定