mongodb - 复合文字使用无键字段

标签 mongodb go

我正在为 golang 使用新的官方 mongodb 驱动程序。我创建了一个复杂的查询来将数据插入 mongo db,然后根据元素值对其进行排序。我正在使用一个过滤器,其中我使用以下方法创建了 bson 类型:-

filter := bson.D{{"autorefid", "100"}}

但它显示警告说:

primitive.E composite literal uses unkeyed fields

这些警告在我的代码中造成了困惑。

最佳答案

可以通过将检查标志设置为 false 来停止警告。

$ go doc cmd/vet

By default all checks are performed. If any flags are explicitly set to true, only those tests are run. Conversely, if any flag is explicitly set to false, only those tests are disabled. Thus -printf=true runs the printf check, -printf=false runs all checks except the printf check.

Unkeyed composite literals

Flag: -composites

Composite struct literals that do not use the field-keyed syntax.

但警告是由于在 primitive.E struct 中设置值时未提供键名。

primitive.E 结构设置键将删除警告消息。例如

filter := bson.D{primitive.E{Key: "autorefid", Value: "100"}}

Package primitive contains types similar to Go primitives for BSON types can do not have direct Go primitive representations.

type E struct {
    Key   string
    Value interface{}
}

E represents a BSON element for a D. It is usually used inside a D.

欲了解更多信息,请查看 primitive.E

关于mongodb - 复合文字使用无键字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54548441/

相关文章:

c# - .NET MongoDB C# 驱动程序不支持转换对象

c# - 如何在反序列化的 MongoDB 文档中获取对父对象的引用?

mongodb - 在 MongoDB 中获取数组的第一个和最后一个元素

mongodb - 根据 bool 值进行编辑或创建

go - 在构建过程中运行命令行工具

go - 如何将 JSON 对象验证为无序列表?

mongodb - 在 $lookup 之后将原始对象数组合并到 "as"字段中

mongodb - 在 mongodb shell 中如何检索下一个结果?

python - 带有 Web 后端的日志存储

go - 类型定义和接口(interface)转换