我想通过找到一个数据_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)
}
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
}
Run Code Online (Sandbox Code Playgroud)
它只是给我一个回报0.
但我可以使用pytmongo和python找到它.
import requests
import pymongo
from pymongo import MongoClient
from bson.objectid import ObjectId
from pprint …Run Code Online (Sandbox Code Playgroud) 我尝试的ObjectId通过使用下面的代码获取MongoDB的纪录,但继续得到未发现通过err.Error()
以下是我的mongo收集样本
{ "_id" : ObjectId("5a2a75f777e864d018131a59"), "callDate" : "22/12/2017", "time" : "16.25", "callType" : "a", "position" : "aaa", "description" : "aaaaaa", "qty" : 2, "estimatedDuration" : 2.3, "estimatedOvertime" : 3.44, "rate" : 4, "laborExtension" : 3 }
{ "_id" : ObjectId("5a2a75f877e864d018131a5b"), "callDate" : "22/12/2017", "time" : "16.25", "callType" : "a", "position" : "aaa", "description" : "aaaaaa", "qty" : 2, "estimatedDuration" : 2.3, "estimatedOvertime" : 3.44, "rate" : 4, "laborExtension" : 3 }
{ "_id" : ObjectId("5a2a75fa77e864d018131a5d"), "callDate" …Run Code Online (Sandbox Code Playgroud)