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

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

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

问题

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

这是文档的样子。

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


func GetLatestBlockFromMongoDB() int{
	if contains(ReturnBlocksExists(), "blocks") == true {
		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
		var blockheights []models.Block
		defer cancel()
	
		options := options.Find().SetProjection(bson.M{"block.header.blockNumber": 1})

		options.SetSort(bson.D{{"block.header.blockNumber", -1}})

		options.SetLimit(1)
	
		results, err := blocksCollections.Find(context.TODO(), bson.D{}, options)
	
		if err != nil {
			fmt.Println(err)
		}
	
		//以最优方式从数据库中读取
		defer results.Close(ctx)
		for results.Next(ctx) {
			var singleBlock models.Block
			if err = results.Decode(&singleBlock); err != nil {
				fmt.Println(err)
			}
	
			blockheights = append(blockheights, singleBlock)
		}
		fmt.Println("%v", blockheights[0].Block.Header.BlockNumber)
		if len(strings.TrimSpace(blockheights[0].Block.Header.BlockNumber)) > 0{
			i, err := strconv.Atoi(blockheights[0].Block.Header.BlockNumber)
			if err != nil {
				glog.Fatalf("%v", err)
			}
			return i
		} else {
			return 0
		}
	} else {
		return 0
	}
}

我如何获得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中找到一个字段的最大值?


func GetLatestBlockFromMongoDB() int{
	if contains(ReturnBlocksExists(), "blocks") == true {
		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
		var blockheights []models.Block
		defer cancel()
	
		options := options.Find().SetProjection(bson.M{"block.header.blockNumber": 1})

		options.SetSort(bson.D{{"block.header.blockNumber", -1}})

		options.SetLimit(1)
	
		results, err := blocksCollections.Find(context.TODO(), bson.D{}, options)
	
		if err != nil {
			fmt.Println(err)
		}
	
		//reading from the db in an optimal way
		defer results.Close(ctx)
		for results.Next(ctx) {
			var singleBlock models.Block
			if err = results.Decode(&singleBlock); err != nil {
				fmt.Println(err)
			}
	
			blockheights = append(blockheights, singleBlock)
		}
		fmt.Println("%v", blockheights[0].Block.Header.BlockNumber)
		if len(strings.TrimSpace(blockheights[0].Block.Header.BlockNumber)) > 0{
			i, err := strconv.Atoi(blockheights[0].Block.Header.BlockNumber)
			if err != nil {
				glog.Fatalf("%v", err)
			}
			return i
		} else {
			return 0
		}
	} else {
		return 0
	}
}

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":

例如:

package main

import (
	"fmt"
)

func findMax(a []string) string {
	m := a[0]
	for _, value := range a {
		if value > m {
			m = value
		}
	}
	return m
}

func main() {
	blocks := []string{"999", "2000", "3000"}
	ma := findMax(blocks)
	fmt.Println(ma)
}
$ go run .
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:

package main

import (
	"fmt"
)

func findMax(a []string) string {
	m := a[0]
	for _, value := range a {
		if value > m {
			m = value
		}
	}
	return m
}

func main() {
	blocks := []string{"999", "2000", "3000"}
	ma := findMax(blocks)
	fmt.Println(ma)
}
$ go run .
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:

确定