mgo mongodb读/写示例

时间:2018-05-19 07:48:40

标签: mongodb go mgo

我是mgo的新手,需要一些帮助: 我可以成功地连接并打印出数据库名称,集合名称和项目数量是集合,但是不知道如何在其中打印出内容并回写。 mgo与mongodb shell命令的等价物是什么?

- db.coll.find()
- document=({"user_id" : "xxx","password" :"xxx"....});
- db.coll.insert(document)

/////////////////////////////////////////////// ///////////////////////

package main

import (
    "fmt"
    "time"

    "gopkg.in/mgo.v2"
)

//const MongoDb details
const (
    hosts      = "mongodb.xxx:27017"
    database   = "myinfo"
    username   = "xxxxx"
    password   = "start123"
    collection = "userdetails"
)

func main() {

    info := &mgo.DialInfo{
        Addrs:    []string{hosts},
        Timeout:  60 * time.Second,
        Database: database,
        Username: username,
        Password: password,
    }

    session, err1 := mgo.DialWithInfo(info)
    if err1 != nil {
        panic(err1)
    }

    col := session.DB(database).C(collection)
    datab := session.DB(database)

    count, err2 := col.Count()
    if err2 != nil {
        panic(err2)
    }

    fmt.Println("Database Name:", datab.Name)
    fmt.Println("Collection FullName:", col.FullName)
    fmt.Println(fmt.Sprintf("Documents count: %d", count))

}

这是一个有效的版本:

    package main

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

//const MongoDb details
const (
    hosts      = "xxx:27017"
    database   = "myinfo"
    username   = "xxxx"
    password   = "start123"
    collection = "userdetails2"
)


type UserDetails struct {
        _id        bson.ObjectId `bson:"_id,omitempty"`
        name    string
        phone   string
}

func main() {

    info := &mgo.DialInfo{
        Addrs:    []string{hosts},
        Timeout:  60 * time.Second,
        Database: database,
        Username: username,
        Password: password,
    }

    session, err1 := mgo.DialWithInfo(info)
    if err1 != nil {
        panic(err1)
    }

    col := session.DB(database).C(collection)
    datab := session.DB(database)

    count, err2 := col.Count()
    if err2 != nil {
        panic(err2)
    }


        fmt.Println("Database Name:", datab.Name)
        fmt.Println("Collection FullName:", col.FullName)
        fmt.Println(fmt.Sprintf("Documents count: %d", count))

        var userDetail []bson.M
        _ = col.Find(nil).All(&userDetail)
        for _, v := range userDetail {
        fmt.Println(v)
}

}

2 个答案:

答案 0 :(得分:0)

试试这个:

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

type UserDetails struct {
    Id bson.ObjectId `bson:"_id,omitempty"`
    UserId string `bson:"user_id"`
    Password string `bson:"password"`
}
userDetails := []UserDetails{}
query := bson.M{
    "user_id": "xxx",
    "password" :"xxx"
}
err := col.Find(query).All(&userDetails)
if err != nil {
    return
}
for _, userDetail := range userDetails {
    fmt.Println(userDetail)
}

我没有测试过这段代码。这只是一个例子。

答案 1 :(得分:0)

这是一个相当简单的mgo包示例:https://gist.github.com/border/3489566

我建议通过官方文档了解详细信息和参考资料。 https://godoc.org/github.com/globalsign/mgo#Collection.Find

请注意,不再维护"gopkg.in/mgo.v2/bson"个包。 https://github.com/globalsign/mgo是该软件包的一个分支,由社区维护。