英文:
Obtain ObjectIdHex value from mgo query
问题
我对Go语言还不太熟悉,虽然我在Stack Overflow上看到了很多类似的问题,但我无法复现一些提问者所要求的输出结果(这个答案看起来最接近)。
我正在做一些相当简单的事情,我正在访问MongoDB中的一个users
集合,我只想将_id
的值作为字符串返回。我最终要将这些_id
推送到NSQ,但这是我任务的主要部分。
var users []bson.M
err = sess.DB("db_name").C("users").Find(bson.M{}).All(&users)
if err != nil {
os.Exit(1)
}
for _, user := range users {
fmt.Printf("%+v \n", user["_id"])
}
今天的输出结果是:
ObjectIdHex("537f700b537461b70c5f0000")
ObjectIdHex("537f700b537461b70c600000")
ObjectIdHex("537f700b537461b70c610000")
ObjectIdHex("537f700b537461b70c620000")
我查看了bson#m文档,并认为我正确地使用了map来提取值。所以我认为,我的查询结果是:
{"_id" : ObjectIdHex("Some_ID")}
但是,如果ObjectIdHex("ID")是值,我该如何简单地获取其中的字符串。
所以理想的输出结果是:
"537f700b537461b70c5f0000"
"537f700b537461b70c600000"
"537f700b537461b70c610000"
"537f700b537461b70c620000"
英文:
I'm still new to go and while I see multiple questions on SO similar to this, I'm unable to reproduce the output some OP's had requested (this answer looking the closest).
I'm doing something fairly simple, I'm hitting a users
collection in mongo and all I want to do is get the _id
value back as a string. I'm going to eventually push these _id
's up to NSQ but that's the brunt of my task.
var users []bson.M
err = sess.DB("db_name").C("users").Find(bson.M{}).All(&users)
if err != nil {
os.Exit(1)
}
for _, user := range users {
fmt.Printf("%+v \n", user["_id"])
}
Today this outputs:
ObjectIdHex("537f700b537461b70c5f0000")
ObjectIdHex("537f700b537461b70c600000")
ObjectIdHex("537f700b537461b70c610000")
ObjectIdHex("537f700b537461b70c620000")
I went through the bson#m docs and thought I was correctly using the map in order to extra the value. So I think, my query results in:
{"_id" : ObjectIdHex("Some_ID") }
but if ObjectIdHex("ID") is the value, how do I simply get the string within there.
So ideal output:
"537f700b537461b70c5f0000"
"537f700b537461b70c600000"
"537f700b537461b70c610000"
"537f700b537461b70c620000"
答案1
得分: 11
与键"_id"
关联的值是bson.ObjectId
类型,它只是一个string
。
bson.M
是类型为map[string]interface{}
的类型,因此您需要类型断言将id作为ObjectId
获取:
objid, ok := m["_id"].(ObjectId)
if !ok {
panic("Not ObjectId")
}
ObjectId
有一个ObjectId.Hex()
方法,它会返回您想要的内容:对象id作为一个"纯"十六进制字符串:
fmt.Println(objid.Hex())
其他选择
objid
可以直接转换为string
,因为它的底层类型是string
。因此,您可以使用许多其他选项将其转换为十六进制string
:
hexid := fmt.Sprintf("%x", string(objid))
如果您只想打印它,可以直接这样做:
fmt.Printf("%x", string(objid))
**注意:**将其转换为string
很重要,否则fmt
包将调用其String()
方法,结果是一个类似ObjectIdHex("537f700b537461b70c5f0000")
的字符串,这显然不是您想要的。
或者您可以使用encoding/hex
包和hex.EncodeToString()
函数:
hexid := hex.EncodeToString([]byte(objid))
英文:
The value associated with key "_id"
is of type bson.ObjectId
which is simply a string
.
bson.M
is a type map[string]interface{}
, so you need Type assertion to get the id as an ObjectId
:
objid, ok := m["_id"].(ObjectId)
if !ok {
panic("Not ObjectId")
}
And the ObjectId
has a ObjectId.Hex()
method which returns exactly what you want: the object id as a "pure" hex string:
fmt.Println(objid.Hex())
Alternatives
objid
can simply be converted to string
because its underlying type is string
. So you can use a number of further options to convert it to a hex string
:
hexid := fmt.Sprintf("%x", string(objid))
If you just want to print it, you can do directly:
fmt.Printf("%x", string(objid))
Note: Converting it to string
is important else the fmt
package would call its String()
method which results in a string like ObjectIdHex("537f700b537461b70c5f0000")
and this is what would be converted to hex which is clearly not what you want.
Alternatively you can use the encoding/hex
package and the hex.EncodeToString()
function:
hexid := hex.EncodeToString([]byte(objid))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论