Codertjay
Codertjay

Reputation: 997

Golang bson.E 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

Upvotes: 2

Views: 1269

Answers (3)

undefine97
undefine97

Reputation: 177

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})

Upvotes: 0

Codertjay
Codertjay

Reputation: 997

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})

Upvotes: 0

phonaputer
phonaputer

Reputation: 1530

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.

Upvotes: 6

Related Questions