我正在尝试使用 ObjectId 进行查询,通常在 mongodb 中你会做这样的事情
db.collection.findOne({"_id":objectid("5d9d90e5ed645489aae6df64")})
当我执行正常查询时,这会起作用,但在 go lang 中,它给出的值是
ObjectIdHex("5d9d90e5ed645489aae6df64")
相反,这不会导致有效的查询。
我已经多次阅读 mgo 文档并尝试使用
bson.ObjectId("5d9d90e5ed645489aae6df64")
但它仍然使它成为我不明白的十六进制。我尝试了几种不同的组合,但它们几乎只是在黑暗中拍摄。
Go 语言处理程序
package userhandlers
import (
"log"
"net/http"
//"fmt"
//"go.mongodb.org/mongo-driver/bson/primitive"
//"go.mongodb.org/mongo-driver/bson"
"labix.org/v2/mgo/bson"
//Services
databaseservice "malikiah.io/services/databaseService"
passwordservice "malikiah.io/services/passwordService"
//Structs
userstructs "malikiah.io/structs/userStructs"
databasestructs "malikiah.io/structs/databaseStructs"
)
func LoginHandler(response http.ResponseWriter, request *http.Request) {
response.Header().Set("Content-Type", "application/json")
response.WriteHeader(http.StatusOK)
databaseQuery := databasestructs.Find{
ID: bson.ObjectId(request.FormValue("_id")),
MongoCollection: "users",
Criteria: "_id",
CriteriaValue: "",
FindAll: false,
}
log.Println(databaseQuery)
databaseservice.Login(databaseQuery)
}
子衿沉夜
相关分类