Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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
使用$gt过滤器和go mongodb驱动程序搜索文档_Mongodb_Date_Datetime_Go_Filter - Fatal编程技术网

使用$gt过滤器和go mongodb驱动程序搜索文档

使用$gt过滤器和go mongodb驱动程序搜索文档,mongodb,date,datetime,go,filter,Mongodb,Date,Datetime,Go,Filter,我陷入了一个可能很简单的问题:如果我在mongodb compass中对其进行过滤(过滤{dateTime:{$gt:new Date(“2020-11-23T12:31:38”)}): 它返回556个文档 尝试在Go中创建一个包含这些文档的游标被证明是相当困难的! 我现在就知道了: cursor, err := coll.Find(context.Background(), bson.M{"dateTime": bson.M{"$gt": "n

我陷入了一个可能很简单的问题:如果我在mongodb compass中对其进行过滤(过滤
{dateTime:{$gt:new Date(“2020-11-23T12:31:38”)}
): 它返回556个文档

尝试在Go中创建一个包含这些文档的游标被证明是相当困难的! 我现在就知道了:

cursor, err := coll.Find(context.Background(), bson.M{"dateTime": bson.M{"$gt": "new Date("+ date + ")"}}, opt)
    if err != nil {
        fmt.Println("Err creting database: ", err)
        return nil, err
    }
    if cursor.Next(context.Background()) {
        fmt.Println("Cursor0!")
        cursor.Next(context.Background())
    }
    cursor1, err := coll.Find(context.Background(), bson.M{}, opt)
    if err != nil {
        fmt.Println("Err creting database: ", err)
        return nil, err
    }
    if cursor1.Next(context.Background()) {
        fmt.Println("Cursor1!")
        cursor.Next(context.Background())
    }.
我尝试过,在其他不同的尝试中,将过滤器放置为
bson.M{“dateTime”:bson.M{“$gt”:date}
,以及其他类似的tryes,但它们也返回了0个文档。
date
变量包含mongodb compass筛选器中使用的日期

我创建了另一个没有过滤器的游标,只是为了控制与mongo的连接是否正常,并查看它在没有过滤器时是否返回任何文档,它确实返回文档。有人知道这个问题的答案吗


谢谢

新日期(“2020-11-23T12:31:38”)
是JavaScript语法。您需要使用正确的Go语法来创建时间戳。

问题是我处理的集合不止一个,其中一个将日期保存为字符串,另一个将日期保存为日期。在将日期保存为字符串的情况下,不足为奇,我们也必须将日期作为字符串发送,请在mongo中将日期作为日期进行逻辑处理;复制/粘贴文本,完成!谢谢你的提醒