Golang中的bson.E未被bson包声明。

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

Golang bson.E not declared by package bson

问题

目前正在进行一个 Golang 项目,但在一些控制器中遇到了问题:

package controller

import (
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"gopkg.in/mgo.v2/bson"
)

var updatedObj primitive.D
updatedObj = append(updatedObj, bson.E{"table", order.Table_id})

我总是得到错误信息:(bson.E) E is not declared by package bson

英文:

Currently working on a Golang project but in some controllers i get

package controller
import (   	
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"gopkg.in/mgo.v2/bson"
)

var updatedObj primitive.D
updatedObj = append(updatedObj, bson.E{"table", order.Table_id})

I always get (bson.E) E is not declared by package bson

答案1

得分: 6

看起来你导入了错误的bson包。

如你所见这里gopkg.in/mgo.v2/bson不包含"E"类型。

根据你使用的其他包,我认为你可能想要使用这个包go.mongodb.org/mongo-driver/bson。你正在使用的primitive包是这个包的子包,所以我认为这两个应该可以正确配合使用。

英文:

It looks like you are importing the wrong bson package.

As you can see here, gopkg.in/mgo.v2/bson does not include the "E" type.

Based on the other packages you are using, I think perhaps you want this one: go.mongodb.org/mongo-driver/bson? The primitive package you are using is a subpackage of this package so I think the two should work together correctly.

答案2

得分: 0

我认为这是因为bson包中没有包含任何E(因为它正在读取其他包:gopkg.in/mgo.v2/bson)。

你可以尝试像这样的代码:

package controller
import (    
    "go.mongodb.org/mongo-driver/bson/primitive"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "gopkg.in/mgo.v2/bson"
)

var updatedObj primitive.D
updatedObj = append(updatedObj, primitive.E{"table", order.Table_id})
英文:

I think it is because bson doesn't have any E included in their package (because it is reading the other package: gopkg.in/mgo.v2/bson).

You can try something like this:

package controller
import (    
    "go.mongodb.org/mongo-driver/bson/primitive"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "gopkg.in/mgo.v2/bson"
)

var updatedObj primitive.D
updatedObj = append(updatedObj, primitive.E{"table", order.Table_id})

答案3

得分: 0

根据https://stackoverflow.com/users/13415116/phonaputer的说法,

我实际上导入了错误的bson包

import (
    "gopkg.in/mgo.v2/bson"
)

这样解决了错误,并且变得正确

import (
    "go.mongodb.org/mongo-driver/bson"
)
var updatedObj primitive.D
updatedObj = append(updatedObj, bs.E{"table", order.Table_id})
英文:

According to https://stackoverflow.com/users/13415116/phonaputer

I actually imported the wrong package for bson

  import (
    	"gopkg.in/mgo.v2/bson"
    )

This solved the error and it became correct

import (
    	"go.mongodb.org/mongo-driver/bson"
    )
    var updatedObj primitive.D
    updatedObj = append(updatedObj, bs.E{"table", order.Table_id})

huangapple
  • 本文由 发表于 2022年8月3日 08:29:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/73215104.html
匿名

发表评论

匿名网友

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

确定