如何在golang和mongodb中用id查找

如何在golang和mongodb中用id查找

本文介绍了如何在golang和mongodb中用id查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用ObjectIdHex获取值并进行更新并查看结果。我正在使用mongodb和golang.But下面的代码不能按预期工作

 包主

进口(
fmt
gopkg.in/mgo.v2
gopkg.in/mgo.v2/bson


类型Person结构{
Id bson.ObjectId`json:idbson:_ id,omitempty`
名称字符串
电话字符串
}

func checkError(err error){
if err!= nil {
panic(err)
}
}

const(
DB_NAME =gotest
DB_COLLECTION =pepole_new1


func main(){
session,err:= mgo.Dial(localhost)
checkError(err)
defer session.Close()
$ b $ session.SetMode(mgo.Monotonic,true)

c:= session.DB DB_NAME).C(DB_COLLECTION)
err = c.DropCollection()
checkError(err)
$ b ale:= Person {Name:Ale,Phone:555- 5555}
cla:= Person {姓名:Cla,电话:555-1234-2222}
kasaun := Person {姓名:kasaun,电话:533-12554-2222}
chamila:= Person {姓名:chamila,电话:533-545-6784}

fmt.Println(插入)
err = c.Insert(& ale,& cla,& kasaun,& chamila)
checkError(err)

fmt.Println(findbyID)
var resultsID [] Person
// err = c.FindId(bson.ObjectIdHex(56bdd27ecfa93bfe3d35047d))。One(& resultsID)
err = c.FindId(bson.M {Id:bson.ObjectIdHex(56bdd27ecfa93bfe3d35047d)})。(& resultsID)
checkError(err)
if err!= nil {
panic(err)
}
fmt.Println(Phone:,resultsID)



fmt.Println( Queryingall)
var results [] Person
err = c.Find(nil).All(& results)

if err!= nil {
panic (err)
}
fmt.Println(Results All:,results)


}

FindId(bson.M {Id:bson.ObjectIdHex(56bdd27ecfa93bfe (& resultID)对我来说不起作用,并给我下面的输出

 插入
查询所有
结果全部:[{ObjectIdHex(56bddee2cfa93bfe3d3504a1)Ale 555-5555} {ObjectIdHex(56bddee2cfa93bfe3d3504a2)Cla 555-1234-2222} {ObjectIdHex(56bddee2cfa93bfe3d3504a3 )kasaun 533-12554-2222} {ObjectIdHex(56bddee2cfa93bfe3d3504a4)chamila 533-545-6784}]
findbyID
panic:找不到

goroutine 1 [running]:
main.checkError(0x7f33d524b000,0xc8200689b0)

我该如何解决这个问题?我需要使用oid来获得价值,并且做更新,我该如何做到这一点

它应该是 _id not Id :

  c.FindId (bson.M {_ id:bson.ObjectIdHex(56bddecfa93bfe3d35047d)})


I need get values using ObjectIdHex and do update and also view the result. I'm using mongodb and golang.But following code doesn't work as expected

package main

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

type Person struct {
    Id      bson.ObjectId `json:"id" bson:"_id,omitempty"`
    Name  string
    Phone string
}

func checkError(err error) {
    if err != nil {
        panic(err)
    }
}

const (
    DB_NAME       = "gotest"
    DB_COLLECTION = "pepole_new1"
)

func main() {
    session, err := mgo.Dial("localhost")
    checkError(err)
    defer session.Close()

    session.SetMode(mgo.Monotonic, true)

    c := session.DB(DB_NAME).C(DB_COLLECTION)
    err = c.DropCollection()
    checkError(err)

    ale := Person{Name:"Ale", Phone:"555-5555"}
    cla := Person{Name:"Cla", Phone:"555-1234-2222"}
    kasaun := Person{Name:"kasaun", Phone:"533-12554-2222"}
    chamila := Person{Name:"chamila", Phone:"533-545-6784"}

    fmt.Println("Inserting")
    err = c.Insert(&ale, &cla, &kasaun, &chamila)
    checkError(err)

    fmt.Println("findbyID")
    var resultsID []Person
    //err = c.FindId(bson.ObjectIdHex("56bdd27ecfa93bfe3d35047d")).One(&resultsID)
    err = c.FindId(bson.M{"Id": bson.ObjectIdHex("56bdd27ecfa93bfe3d35047d")}).One(&resultsID)
    checkError(err)
    if err != nil {
        panic(err)
    }
    fmt.Println("Phone:", resultsID)



    fmt.Println("Queryingall")
    var results []Person
    err = c.Find(nil).All(&results)

    if err != nil {
        panic(err)
    }
    fmt.Println("Results All: ", results)


}

FindId(bson.M{"Id": bson.ObjectIdHex("56bdd27ecfa93bfe3d35047d")}).One(&resultsID) didn't work for me and giving me following output

Inserting
Queryingall
Results All:  [{ObjectIdHex("56bddee2cfa93bfe3d3504a1") Ale 555-5555} {ObjectIdHex("56bddee2cfa93bfe3d3504a2") Cla 555-1234-2222} {ObjectIdHex("56bddee2cfa93bfe3d3504a3") kasaun 533-12554-2222} {ObjectIdHex("56bddee2cfa93bfe3d3504a4") chamila 533-545-6784}]
findbyID
panic: not found

goroutine 1 [running]:
main.checkError(0x7f33d524b000, 0xc8200689b0)

How can i fix this problem? i need get value using oid and do update also how can i do that

解决方案

It should be _id not Id:

c.FindId(bson.M{"_id": bson.ObjectIdHex("56bdd27ecfa93bfe3d35047d")})

这篇关于如何在golang和mongodb中用id查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 12:40