<small id='vfscj'></small><noframes id='vfscj'>

    • <bdo id='vfscj'></bdo><ul id='vfscj'></ul>
    <tfoot id='vfscj'></tfoot><legend id='vfscj'><style id='vfscj'><dir id='vfscj'><q id='vfscj'></q></dir></style></legend>

      <i id='vfscj'><tr id='vfscj'><dt id='vfscj'><q id='vfscj'><span id='vfscj'><b id='vfscj'><form id='vfscj'><ins id='vfscj'></ins><ul id='vfscj'></ul><sub id='vfscj'></sub></form><legend id='vfscj'></legend><bdo id='vfscj'><pre id='vfscj'><center id='vfscj'></center></pre></bdo></b><th id='vfscj'></th></span></q></dt></tr></i><div id='vfscj'><tfoot id='vfscj'></tfoot><dl id='vfscj'><fieldset id='vfscj'></fieldset></dl></div>

      1. spring-data-mongo - 可选查询参数?

        spring-data-mongo - optional query parameters?(spring-data-mongo - 可选查询参数?)

      2. <tfoot id='iptQy'></tfoot>
            <tbody id='iptQy'></tbody>

        • <legend id='iptQy'><style id='iptQy'><dir id='iptQy'><q id='iptQy'></q></dir></style></legend>
            <bdo id='iptQy'></bdo><ul id='iptQy'></ul>

              <i id='iptQy'><tr id='iptQy'><dt id='iptQy'><q id='iptQy'><span id='iptQy'><b id='iptQy'><form id='iptQy'><ins id='iptQy'></ins><ul id='iptQy'></ul><sub id='iptQy'></sub></form><legend id='iptQy'></legend><bdo id='iptQy'><pre id='iptQy'><center id='iptQy'></center></pre></bdo></b><th id='iptQy'></th></span></q></dt></tr></i><div id='iptQy'><tfoot id='iptQy'></tfoot><dl id='iptQy'><fieldset id='iptQy'></fieldset></dl></div>

              <small id='iptQy'></small><noframes id='iptQy'>

                  本文介绍了spring-data-mongo - 可选查询参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我将 spring-data mongo 与基于 JSON 的查询方法一起使用,但不确定如何在搜索查询中允许可选参数.

                  I am using spring-data mongo with the JSON based query methods, and am unsure how to allow optional parameters in a search query.

                  例如 - 假设我有以下功能

                  For instance - say I had the following function

                  @Query("{ 'name' : {$regex : ?0, $options : 'i'}, 'createdDate' : {$gte : ?1, $lt : ?2 }} }")
                  List<MyItem> getItemsLikeNameByDateRange(String name, Date startDateRange, Date endDateRange);
                  

                  -但我不想应用名称正则表达式匹配,或者如果将 NULL 值传递给方法,则不应用日期范围限制.

                  -but I didnt want to apply the name regex match, or not apply a date range restriction if NULL values were passed to the method.

                  目前看来我可能必须使用 mongoTemplate 构建查询.

                  At the moment it looks like I might have to build the query using the mongoTemplate.

                  是否有任何替代方案 - 或者使用 mongoTemplate 是最佳选择?

                  Are there any alternatives - or is using mongoTemplate the best option?

                  谢谢

                  推荐答案

                  为了在布尔逻辑中实现这一点,我执行以下操作并转换为编程语言中可用的操作

                  To implement this in Boolean logic I do the following and the conversion to operations that are available in programming languages

                  :query != null -> field == :query
                  !(:query != null) || (field == :query)
                  (:query == null) || (field == :query)
                  

                  在普通的 SQL 中,这是这样完成的

                  In plain SQL, this is done as

                  where (null = :query) or (field = :query)
                  

                  在 MongoDB 中,这是通过 $where 完成的

                  In MongoDB this is done through the $where

                  { $where: '?0 == null || this.field == ?0' } 
                  

                  我们可以通过使用 Mongo 操作来加快速度一点,而不是以牺牲一些可读性为代价构建函数的所有内容.不幸的是,它不起作用.

                  We can speed this up a little by using Mongo Operations rather than building everything to the function at the expense of some readability. does not work unfortunately.

                  { $or : [ { $where: '?0 == null' } , { field : ?0 } ] } 
                  

                  所以你拥有的是

                  @Query("{ $or : [ { $where: '?0 == null' } , { field : ?0 } ] }")
                  List<Something> findAll(String query, Pageable pageable);
                  

                  这可以进一步扩展为处理 in/all 子句的数组

                  This can be further expanded to handle arrays for in/all clauses

                  @Query("{ $or : [ { $where: '?0.length == 0' } , { field : { $in : ?0 } } ] }")
                  List<Something> findAll(String query, Pageable pageable);
                  

                  这篇关于spring-data-mongo - 可选查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                • <i id='FpiE6'><tr id='FpiE6'><dt id='FpiE6'><q id='FpiE6'><span id='FpiE6'><b id='FpiE6'><form id='FpiE6'><ins id='FpiE6'></ins><ul id='FpiE6'></ul><sub id='FpiE6'></sub></form><legend id='FpiE6'></legend><bdo id='FpiE6'><pre id='FpiE6'><center id='FpiE6'></center></pre></bdo></b><th id='FpiE6'></th></span></q></dt></tr></i><div id='FpiE6'><tfoot id='FpiE6'></tfoot><dl id='FpiE6'><fieldset id='FpiE6'></fieldset></dl></div>

                      <tbody id='FpiE6'></tbody>
                  • <tfoot id='FpiE6'></tfoot>
                    1. <legend id='FpiE6'><style id='FpiE6'><dir id='FpiE6'><q id='FpiE6'></q></dir></style></legend>

                      <small id='FpiE6'></small><noframes id='FpiE6'>

                          • <bdo id='FpiE6'></bdo><ul id='FpiE6'></ul>