草庐IT

java - 带日期的 mongo 查询无法按预期工作

coder 2023-11-06 原文

我有一个由下面的 java 驱动程序生成的查询:

{ "$and" : [
    { "source_ip" : "10.0.71.218"} , 
    { "login" : { "$lte" : { "$date" : "2016-06-03T00:17:18.000Z"}}} , 
    { "$or" : [ 
        { "logout" : { "$exists" : false}} , 
        { "logout" : { "$gte" : { "$date" : "2016-06-03T00:17:18.000Z"}}}
        ]
    }
    ]
}

这不会正确返回数据。但是,用 ISODate() 替换 $date 可以正确获取数据。我确实理解驱动程序对 JSON 的“严格”使用。但是我不确定我在下面的 Java 代码中遗漏了什么/做错了什么:

BasicDBObject logoutfilterQuery = new BasicDBObject();
List<BasicDBObject> lst_logoutfilter = new ArrayList<BasicDBObject>();
lst_logoutfilter.add(new BasicDBObject("logout", new BasicDBObject("$exists", false)));
lst_logoutfilter.add(new BasicDBObject("logout", new BasicDBObject("$gte", logtime)));

logoutfilterQuery.put("$or", lst_logoutfilter);

BasicDBObject fetch_pppoe_user_query = new BasicDBObject();
List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
obj.add(new BasicDBObject("source_ip", sourceIP));
obj.add(new BasicDBObject("login", new BasicDBObject("$lte", logtime)));
obj.add(logoutfilterQuery);

fetch_pppoe_user_query.put("$and", obj);
BasicDBObject fields = new BasicDBObject();
fields.put("user", 1);
fields.put("login", 1);
fields.put("logout", 1);
DBCursor cursor = collection.find(fetch_pppoe_user_query, fields);

任何指点将不胜感激。

示例文档:

{
    "_id" : ObjectId("5753f6821faca4f72daeb374"),
    "source_ip" : "10.0.181.163",
    "user" : "xyz@abc.com",
    "location" : "SOMEPLACE",
    "login" : ISODate("2016-06-01T12:43:35.000Z"),
    "logout" : ISODate("2016-06-01T12:45:18.000Z"),
    "connectionTimeInSeconds" : NumberLong(103),
    "datatransferIn" : NumberLong(54),
    "datatransferOut" : NumberLong(58),
    "packetsIn" : NumberLong(3),
    "packetsOut" : NumberLong(4)
}

最佳答案

这是考虑时区的代码。我认为问题在于处理时区。通常,Mongo DB 以 UTC 时区存储日期。

请根据您的要求相应地更改集合名称和数据库名称。

 public static void main(String[] args) throws ParseException {
    String sourceIP = "10.0.181.163";

    SimpleDateFormat logtimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS\'Z\'");
    logtimeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    Date logtime = logtimeFormat.parse("2016-06-01T12:45:01.000Z");

    MongoClient client = new MongoClient();
    MongoDatabase database = client.getDatabase("localhost");
    BasicDBObject logoutfilterQuery = new BasicDBObject();
    List<BasicDBObject> lst_logoutfilter = new ArrayList<BasicDBObject>();
    lst_logoutfilter.add(new BasicDBObject("logout", new BasicDBObject("$exists", false)));
    lst_logoutfilter.add(new BasicDBObject("logout", new BasicDBObject("$gte", logtime)));

    logoutfilterQuery.put("$or", lst_logoutfilter);

    BasicDBObject fetch_pppoe_user_query = new BasicDBObject();
    List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
    obj.add(new BasicDBObject("source_ip", sourceIP));
    obj.add(new BasicDBObject("login", new BasicDBObject("$lte", logtime)));
    obj.add(logoutfilterQuery);
    fetch_pppoe_user_query.put("$and", obj);
    System.out.println(fetch_pppoe_user_query);
    FindIterable<Document> findDoc = database.getCollection("dateissue").find(fetch_pppoe_user_query);
    for (Document doc : findDoc) {
        System.out.println(doc.toJson());
    }
} 

下面是我的 Mongo Collection 。请注意日期值。

关于java - 带日期的 mongo 查询无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37740837/

有关java - 带日期的 mongo 查询无法按预期工作的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  4. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  7. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  8. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  9. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  10. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

随机推荐