mgo

Cannot retrieve “_id” value using mgo with golang

家住魔仙堡 提交于 2019-12-01 02:47:07
This is my struct definition: type Article struct { Id bson.ObjectId `json:"id" bson:"_id,omitempty"` Title string `json:"title"` Author string `json:"author"` Date string `json:"date"` Tags string `json:"tags"` Content string `json:"content"` Status string `json:"status"` } This is the method I get my data from database: func AllArticles() []Article { articles := []Article{} err := c_articles.Find(bson.M{}).All(&articles) if err != nil { panic(err) } return articles } This is one piece of object stored in database: { "_id" : ObjectId( "5281b83afbb7f35cb62d0834" ), "title" : "Hello1", "author"

Cannot retrieve “_id” value using mgo with golang

拟墨画扇 提交于 2019-11-30 21:41:07
问题 This is my struct definition: type Article struct { Id bson.ObjectId `json:"id" bson:"_id,omitempty"` Title string `json:"title"` Author string `json:"author"` Date string `json:"date"` Tags string `json:"tags"` Content string `json:"content"` Status string `json:"status"` } This is the method I get my data from database: func AllArticles() []Article { articles := []Article{} err := c_articles.Find(bson.M{}).All(&articles) if err != nil { panic(err) } return articles } This is one piece of

How to pass an int slice to “$in” using mgo

爷,独闯天下 提交于 2019-11-30 19:14:16
问题 I'm having a bit of trouble creating a query using the bson functionality of mgo . I'm simply trying to do {'search_id': {'$in': [1,2,4,7,9]}} , but I can't work out how to do it in mgo . I have a slice of int s, and tried passing that directly: toRemove := []int{1,2,4,7,9} err = coll.Remove(bson.M{"search_id": bson.M{"$in": toRemove}}) I saw another post which suggested I needed to use []interface{} , but that doesn't work either: toRemoveI := make([]interface{}, len(toRemove)) for idx, val

Golang mongodb使用

不打扰是莪最后的温柔 提交于 2019-11-30 18:06:16
资料 官方文档 官方驱动github 之前一直使用mgo,但是已经不维护了,(mgo:是MongoDB的Go语言驱动,它用基于Go语法的简单API实现了丰富的特性,并经过良好测试。使用起来很顺手,文档足够),因此转到mongodb官方driver。mongo-go-driver:官方的驱动,设计的很底层,因此扩展性比较好,但是使用复杂度有一定提高,并且支持事务。 入门 连接 import ( "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017")) go的连接驱动一般都支持连接池,果不其然,mongodb也是,我们查看源码发现可以设置 options对象里包含连接池大小配置,可以方便开发根据环境进行配置。 MaxPoolSize *uint64 MinPoolSize *uint64 查询 查询相对复杂,会返回一个迭代器,这时候可以封装一个共用方法进行处理 ctx, _ = context.WithTimeout(context.Background(), 30*time.Second) cur, err

How to check if collection exists or not MongoDB Golang

十年热恋 提交于 2019-11-30 14:20:01
I am new to GO language and I am using MongoDB with it. I am creating a backend for an application and its frontend on Angular 4 . I want to check if collection exists or not. Here is my code and I have checked it using nil . collection := GetCollection("users") fmt.Println("collection", collection) if collection == nil { fmt.Println("Collection is empty") } I have created a GetCollection function which return a collection when we pass it a collection name. So when if there is no collection how can I check that if it exists or not? I have tried many things but failed. icza You may simply use

Accessing MongoDB from Go

自古美人都是妖i 提交于 2019-11-30 09:50:04
问题 I am accessing MongoDB using Go as follows: var configRes *clientConfigData err := clientDB. C(clientConfigCollection). Find(bson.M{}). One(&configRes) if err != nil { return nil, errors.Wrap(err, "finding config collection") } Where type clientConfigData struct { SMTPAssoc int `bson:"smtp_assoc"` PlanType string `bson:"plan_type"` EndDate string `bson:"end_date"` } Now since EndDate in MongoDB is stored as string so I declared EndDate as string . But I need to access this date as Go Time in

Go: how to run tests for multiple packages?

社会主义新天地 提交于 2019-11-30 08:53:42
I have multiple packages under a subdirectory under src/, running the tests for each package with go test is working fine. When trying to run all tests with go test ./... the tests are running but it fails.. the tests are running against local database servers, each test file has global variables with db pointers. I tried to run the tests with -parallel 1 to prevent contention in the db, but the tests still fail. what can be the issue here? EDIT: some tests are failing on missing DB entries, I completely clear the DB before and after each test. the only reason I can think of why this is

server returned error on SASL authentication step: Authentication failed

浪子不回头ぞ 提交于 2019-11-29 20:33:40
The following is my MongoDB connection dial from GoLang. But it's returning a panic " server returned error on SASL authentication step: Authentication failed. ". My username, password, hostAddrs and dbName are correct. What am I missing here? dbName: = os.Getenv("ENV_DBNAME") userName: = os.Getenv("ENV_DBUSER") password: = os.Getenv("ENV_DBPASS") dbHost: = os.Getenv("ENV_DBHOST") mongoDialInfo: = & mgo.DialInfo { Addrs: [] string { dbHost }, Database: dbName, Username: userName, Password: password, Timeout: 60 * time.Second, } sess, err: = mgo.DialWithInfo(mongoDialInfo) if (err != nil) {

How to check if collection exists or not MongoDB Golang

谁说我不能喝 提交于 2019-11-29 20:15:01
问题 I am new to GO language and I am using MongoDB with it. I am creating a backend for an application and its frontend on Angular 4 . I want to check if collection exists or not. Here is my code and I have checked it using nil . collection := GetCollection("users") fmt.Println("collection", collection) if collection == nil { fmt.Println("Collection is empty") } I have created a GetCollection function which return a collection when we pass it a collection name. So when if there is no collection

How to use new URL from mongodb 3.6 to connect from golang

六月ゝ 毕业季﹏ 提交于 2019-11-29 17:29:19
I tried to connect to mongodb Atlas using golang drivers. tlsConfig := &tls.Config{} var mongoURI = "mongodb+srv://admin:password@prefix.mongodb.net:27017/dbname" dialInfo, err := mgo.ParseURL(mongoURI) if err != nil { panic(err) } dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) { conn, err := tls.Dial("tcp", addr.String(), tlsConfig) return conn, err } session, err := mgo.DialWithInfo(dialInfo) if err != nil { println("error") log.Fatal(err) } _ = session c := session.DB("Token").C("_Users") user := &User{firstName: "username"} err = c.Insert(user) if err != nil { println(