dongnuo6310 2016-12-20 13:56
浏览 93
已采纳

通过mgo按ID查找

I would like to find a data by _id. I know that this data exists and that this _id exist (I've tested it with pymongo).

But the code below doesn't find it:

type id_cookie struct {
    IdCookie int
}

func get_id_mongo() int {
    session, err := mgo.Dial("127.0.0.1")
    if err != nil {
        panic(err)
    }
    defer session.Close()

    // Optional. Switch the session to a monotonic behavior.
    session.SetMode(mgo.Monotonic, true)

    c := session.DB("id_bag").C("id_cookie")
    data := id_cookie{}
    err2 := c.FindId(bson.M{"_id": bson.ObjectIdHex("58593d1d6aace357b32bb3a1")}).One(&data)
    if (err2 != nil){
        Info.Println("error")
        Info.Println(err2)
    }
    Info.Println(data)
    return data.IdCookie
}

It just returns me a 0.

But I can find it using pytmongo and python.

import requests
import pymongo 
from pymongo import MongoClient
from bson.objectid import ObjectId
from pprint import pprint
client = MongoClient('127.0.0.1', 27017)

import base64



db = client.id_bag
pprint(db.collection_names())
result = db.id_cookie.insert_one(
    { 'IdCookie': 1
    })
print(result.inserted_id)

data = db.id_cookie.find_one({"_id": ObjectId("58593d1d6aace357b32bb3a1")})
print(data)

here are the result :

['id_cookie', 'system.indexes']
58593d2d6aace357b32bb3a3
{'IdCookie': 1, '_id': ObjectId('58593d1d6aace357b32bb3a1')}

Does anyone have any idea?

Edit : i've try with :

err2 := c.FindId(bson.ObjectIdHex("58593d1d6aace357b32bb3a1")).One(&data)

but i still have 0 :

INFO: 2016/12/20 15:42:08 Cookie_Id.go:147: 1
INFO: 2016/12/20 15:42:08 Cookie_Id.go:149: 2
INFO: 2016/12/20 15:42:18 Cookie_Id.go:87: data
INFO: 2016/12/20 15:42:18 Cookie_Id.go:88: {0}
INFO: 2016/12/20 15:42:18 Cookie_Id.go:89: 0
INFO: 2016/12/20 15:42:18 Cookie_Id.go:118: 0
INFO: 2016/12/20 15:42:18 Cookie_Id.go:128: OK
  • 写回答

1条回答 默认 最新

  • duande3134 2016-12-20 14:04
    关注

    You either use Collection.FindId() and then you pass only the id value, or you use Collection.Find() and then you have to specify a value with the field name too:

    err2 := c.FindId(bson.ObjectIdHex("58593d1d6aace357b32bb3a1")).One(&data)
    
    // OR
    err2 := c.Find(bson.M{"_id": bson.ObjectIdHex("58593d1d6aace357b32bb3a1")}).
        One(&data)
    

    If you get no errors, that means the document is found.

    If you always see 0 printed (as the value of the id_cookie.IdCookie field), that means the field in the document holding this id has a different name.

    Use struct tags to tell how it is stored in your MongoDB. E.g. if in your MongoDB it is called "myid", you can map it like this:

    type id_cookie struct {
        IdCookie int `bson:"myid"`
    }
    

    Also note that you should not connect to the MongoDB server every time you want to query some data, instead connect once, and just reuse the session. for details see: mgo - query performance seems consistently slow (500-650ms)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大