douxuan3095 2015-04-11 14:42
浏览 118
已采纳

在mongodb中按日期查询

I'm able to insert an entry into MongoDB using the golang driver gopkg.in/mgo.vs and gopkg.in/mgo.vs/bson but I'm not able to pull it out. In the mongo shell, if I do

db.Items.find({ date : 1428762411980 })

it shows me the entry that I just inserted with the Go code. However, if I try to do the following to fetch it in Go, it's telling me that the record isn't found

    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")
    }

One thing I noticed was that, in the shell, the date is stored as a NumberLong

 "date" : NumberLong("1428762411980")

I'm wondering if I have to do something with the date value that I receive from the form in the fetch function before using it to query the database?

Update

Before saving the data to the db, it comes in as a json string like this

"date":1428762411980

I then decode it into a struct

type blah struct{
    Id bson.ObjectId `json:"id" bson:"_id"`
    Date int64 `json:"date" bson: "date"`

And it gets saved like this (as shown in the shell)

 "date" : NumberLong("1428762411980")
  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 如何判断一个数是否为超级素数
      • ¥15 list的ad方法出错求解答
      • ¥20 关于mmap将物理空间映射到用户空间再落盘时使用O_DIRECT方式会提示bad address的问题如何解决?
      • ¥15 无人机px4下载环境编译出错,如何解决?
      • ¥15 您好,请问您的那个pcap转成pcd文件的matlab代码我下载了运行没问题,也生成了点云图,但是无法存储pcd文件,请问怎么保存导出pcd文件呀
      • ¥260 已有标注好的图像数据集,如何构建深度卷积网络训练分类
      • ¥20 matlab图像错误
      • ¥30 如何写通过uni.uploadFile上传图片到ASP后端接收的代码
      • ¥15 selenium模糊定位
      • ¥15 硬件断点的问题,真的被整无语了