MongoDB 用MongoTemplate查询指定时间范围的数据

mongoDB大于小于符号对应:

> 大于 $gt
< 小于 $lt
>= 大于等于 $gte
<= 小于等于 $lte

要查询同一个时间多个约束可能出现的error:

org.springframework.data.mongodb.InvalidMongoDbApiUsageException:
Due to limitations of the com.mongodb.BasicDocument, you can‘t add a second ‘createdDate‘ expression specified as ‘createdDate:
Document{{$lt=2018-01-06}}‘. Criteria already contains ‘createdDate: Document{{$gte=2017-12-31}}‘.

解决办法:
要查询同一个字段多个约束需要用andOperator:

Query query = new Query(
Criteria.where("ip").is(ip)
.andOperator(
Criteria.where("createdDate").lt(endDate),
Criteria.where("createdDate").gte(startDate)));

原文地址:https://www.cnblogs.com/exmyth/p/10765947.html

时间: 2024-08-30 07:12:39

MongoDB 用MongoTemplate查询指定时间范围的数据的相关文章

MySQL—查询某时间范围的数据

-- 查询今天的数据 select * from `user` where to_days(birthday) = to_days(CURDATE()); -- 查询昨天的数据 select * from `user` where to_days(CURDATE()) - to_days(birthday)<=1; -- 查询最近7天的数据 select * from `user` where birthday > DATE_SUB(CURDATE(),INTERVAL 7 DAY); --

JDBC查询指定条件的数据

使用select语句的条件查询,需要用到where子句. 1 package qddx.JDBC; 2 import java.sql.*; 3 public class QueryById { 4 5 public bbsVo QuerybbsVoById(int id){ 6 bbsVo vo = null; 7 Connection conn = null; 8 PreparedStatement pst = null; 9 ResultSet rs = null; 10 try{ 11

MySQL通过POIN数据类型查询指定范围内数据

SELECT *, AsText(location) as 原始坐标数据, (st_distance(location, point(116.397915,39.908946))*111) AS distance FROM oc_district HAVING distance<100 ORDER BY distance limit 100; 原文地址:https://www.cnblogs.com/Soy-technology/p/10981124.html

MySQL 查询时间段内的数据

先来建表语句: SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for t_user -- ---------------------------- DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `userId` bigint(20) NOT NULL, `fullName` varchar(64) NOT NULL, `use

MongoDB新增及查询数据(一)

新增操作    insert函数会添加一个文档到集合里面.例如我们要登记一个人的信息,首先我们在shell力创建一个局部变量person,其记录了人的姓名和性别,我们通过db.persons.insert(person)想集合persons中插入一条记录,通过db.persons.findOne()查询刚才插入的记录.                                                                              图 7.新增操作插入的时

mysql5.7基础 查询指定表中的所有数据

镇场文:       学儒家经世致用,行佛家普度众生,修道家全生保真,悟易理象数通变.以科技光耀善法,成就一良心博客.______________________________________________________________________________________________________ Operating System:UbuntuKylin 16.04 LTS 64bitmysql: Ver 14.14 Distrib 5.7.17, for Linux (

Oracle使用游标查询指定数据表的所有字段名称组合而成的字符串

应用场合:参考网上查询数据表的所有字段名代码,使用游标生成指定单个表的所有字段名跟逗号组成的用于select  逗号隔开的字段名列表 from字符串等场合. 查询结果输出如下: 当前数据表TB_UD_USER的字段列表字符串为AH,BIRTHPLACE,BM,CELLPHONE,CJGZRQ,DEPARTMENT2,DJJID,GZCX,GZKH,GZSFZH,HJDZ,HYZK,ID,JHRQ,JTZZ,LAFX_LD,LJDZ,LLY,LXDH,NAME,NXDH,POLICENUMBER,

[Spring Data MongoDB]学习笔记--MongoTemplate查询操作

查询操作主要用到两个类:Query, Criteria 所有的find方法都需要一个query的object. 1. 直接通过json来查找,不过这种方式在代码中是不推荐的. BasicQuery query = new BasicQuery("{ age : { $lt : 50 }, accounts.balance : { $gt : 1000.00 }}"); List<Person> result = mongoTemplate.find(query, Perso

mongodb 之慢查询

mongodb中慢查询日志 1.开启查询日志. (1)动态开启,可以选择性的对指定DB开启慢查询日志,但服务器重启后慢查询日志又要人工手动开启. db.setProfilingLevel(1,300) setProfilingLevel有两个参数,第一参数表示慢查询日志开关(可使用的值为0,1,2 0:表示关闭慢查询日志,1:表示开启慢查询日志,2:表示记录所有数据库中的查询), 第二个参数为慢查询阀值,单位是毫秒,默认情况下阀值为100ms. (2).配置文件中开启,永久生效.对所有的数据DB