通过id与mgo查找 [英] Find by id with mgo

查看:103
本文介绍了通过id与mgo查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 _id 查找数据。我知道这个数据存在,并且存在这个 _id (我用pymongo测试过它)。



但是下面的代码没有找到它:

  type id_cookie struct {
IdCookie int
}

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

//可选。将会话切换为单调行为。
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)})。(& data)
if(err2!= nil){
Info.Println(error)
Info.Println(err2)
}
Info.Println(data)
返回data.IdCookie
}

它只是给我一个 0



但我可以使用pytmongo和python找到它。

 导入请求
从pymongo导入pymongo
导入MongoClient
from bson.objectid import ObjectId
from pprint import pprint
client = MongoClient('127.0.0.1',27017)

导入base64



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

data = db.id_cook ie.find_one({_ id:ObjectId(58593d1d6aace357b32bb3a1)})
print(data)

这里是结果:

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

有没有人有任何想法?



编辑:
我试过:

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

但我仍然有0:

 信息:2016/12/20 15:42: 08 Cookie_Id.go:147:1 
信息:2016/12/20 15:42:08 Cookie_Id.go:149:2
信息:2016/12/20 15:42:18 Cookie_Id.go :87:数据
信息:2016/12/20 15:42:18 Cookie_Id.go:88:{0}
信息: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


您可以使用 Collection.FindId() ,然后只传递id值,或者使用 Collection.Find() 然后你必须指定一个字段名称的值:

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

// OR
err2:= c.Find(bson.M {_ id:bson.ObjectIdHex(58593d1d6aace357b32bb3a1)})。
一个(&数据)

如果您没有错误,那意味着文档是找到。



如果您总是看到 0 打印出来(作为 id_cookie的值。 IdCookie 字段),这意味着持有此id的文档中的字段具有不同的名称。

使用建立标签,告诉它如何存储在你的MongoDB中。例如。如果在你的MongoDB中它被称为myid,你可以像这样映射它:

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

还要注意,每次你想查询一些数据时,你不应该连接到MongoDB服务器,而应该连接一次,然后重新使用会话。有关详细信息,请参阅: mgo - 查询性能似乎一直很慢( 500-650ms)


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

解决方案

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)

这篇关于通过id与mgo查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆