我可以使用 golang 驅(qū)動(dòng)程序?qū)⒁粋€(gè)條目插入到 MongoDB 中 gopkg.in/mgo.vs,gopkg.in/mgo.vs/bson但我無(wú)法將其拉出。在 mongo shell 中,如果我這樣做db.Items.find({ date : 1428762411980 })它向我顯示了我剛剛與 Go 代碼一起插入的條目。但是,如果我嘗試執(zhí)行以下操作以在 Go 中獲取它,它會(huì)告訴我找不到該記錄 func fetch(w http.ResponseWriter, r *http.Request){ var result SomeStruct date := r.FormValue("date") err := Items.Find(bson.M{"date":date}).One(&result) ...code omitted... } func Items() *mgo.Collection { return DB().C("Items") } func DB() *mgo.Database { return DBSession().DB("mydb") }我注意到的一件事是,在 shell 中,日期存儲(chǔ)為 NumberLong "date" : NumberLong("1428762411980")我想知道fetch在使用它查詢(xún)數(shù)據(jù)庫(kù)之前,我是否必須對(duì)我從函數(shù)中的表單接收到的日期值做一些事情?更新在將數(shù)據(jù)保存到數(shù)據(jù)庫(kù)之前,它以這樣的 json 字符串形式出現(xiàn)"date":1428762411980然后我將它解碼成一個(gè)結(jié)構(gòu)type blah struct{ Id bson.ObjectId `json:"id" bson:"_id"` Date int64 `json:"date" bson: "date"`它像這樣保存(如外殼所示) "date" : NumberLong("1428762411980")
在mongodb中按日期查詢(xún)
ibeautiful
2021-09-27 20:44:06