mgo mongodb读/写示例

I am new to mgo and would need some help with this: I can succesfully connect and print out db name, collection name and number of item is collection, but do not know how to print out contents in it and write back. What would be an equivalent in mgo to the below mongodb shell commands?

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

}

Here is a version that works:

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

}

Try this:

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

I haven't test this code. It's just an example.

Here's fairly simple example of mgo package: https://gist.github.com/border/3489566

I would suggest to go through the official documentation for details and references. https://godoc.org/github.com/globalsign/mgo#Collection.Find

Note that "gopkg.in/mgo.v2/bson" package is no longer maintained. https://github.com/globalsign/mgo is a fork of that package and maintained by community.