Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
戈朗+;mgo查询mongodb在使用时间时失败,因为缺少';ISODate';_Mongodb_Go_Time_Mgo_Gorilla - Fatal编程技术网

戈朗+;mgo查询mongodb在使用时间时失败,因为缺少';ISODate';

戈朗+;mgo查询mongodb在使用时间时失败,因为缺少';ISODate';,mongodb,go,time,mgo,gorilla,Mongodb,Go,Time,Mgo,Gorilla,我有以下代码从mongodb中检索一些数据- currentDate := time.Now().Format(time.RFC3339) content := database.FindDocuments("content", bson.M{ "$and": []bson.M{ bson.M{"start_date": bson.M{"$lte": currentDate}}, bson.M{"end_date": bson.M{ "$gte": currentDate}}, }}) Fin

我有以下代码从mongodb中检索一些数据-

currentDate := time.Now().Format(time.RFC3339)
content := database.FindDocuments("content", bson.M{ "$and": []bson.M{ bson.M{"start_date": bson.M{"$lte": currentDate}}, bson.M{"end_date": bson.M{ "$gte": currentDate}}, }})
FindDocuments基本上是
MgoSession.DB(Dbname).C(collectionName).Find(query).All(&result)
给了我一个
[]映射[string]接口{}

这给了我null,而在mongo控制台中(使用与currentDate变量返回的值相同的值)-

还我-

{ 
    "_id" : ObjectId("57cff2bc32291a1fa0e79e90"), 
    "image_url" : "www.example.com", 
    "title" : "This is a new content", 
    "start_date" : ISODate("2016-09-06T10:58:54.701+0000"), 
    "end_date" : ISODate("2016-09-10T10:59:04.447+0000"), 
    "type" : "content"
}

尽管使用了正确的时间格式,但为什么会出现此问题?

mgo驱动程序似乎足够聪明,可以正确地将时间转换为mongo日期,所以

currentDate := time.Now()
应该有用。此外,gopkg.in/mgo.v2/bson还提供了助手,可以获得mongo使用的毫秒精度的时间

func Now() time.Time
所以

这个助手函数有简单的源代码

return time.Unix(0, time.Now().UnixNano()/1e6*1e6)
通过这种方式,任何Go时间戳时间。时间可以以毫秒为单位获得

someDate := time.Unix(0, time.someTime.UnixNano()/1e6*1e6)

你好这并不是因为日期不正确。在mongo控制台查询中,我刚刚复制粘贴了从程序控制台看到的currentDate返回的值。当使用这个时,返回的时间值是1473326105000,响应仍然为空。控制台中{“开始日期”:{$lt:1473326105000}、[{“结束日期”:{$gt:1473326105000}]}是否工作?也许mongo文档在毫秒方面是不正确的。currentDate:=time.Now().Unix()或控制台{“开始日期”:{$lt:1473326105},{{“结束日期”:{$gt:1473326105}}}在mongo控制台上,这两个函数也返回null:(@GauravOjha在深入调查后完全重写答案。不便之处,抱歉。谢谢!我刚刚找到了答案。现在()工作!这不是很令人惊讶但很令人沮丧吗?!我也有同样的问题。。。
return time.Unix(0, time.Now().UnixNano()/1e6*1e6)
someDate := time.Unix(0, time.someTime.UnixNano()/1e6*1e6)