当前位置:网站首页>Mongoose condition queries the data of a certain time period
Mongoose condition queries the data of a certain time period
2022-07-28 20:37:00 【Xiao language is scattered】
Mongoose Query the data of a certain period
Today, when writing something, you need to implement a data search function , Among them, there are requirements for searching by time conditions , Write the idea of implementation here .
First , Querying by time is generally to find the data of an interval value , There are mainly the following situations :
- A-B Data between ;
for example :2020 year 6 month 1 Number of data , the truth is that 6 month 1 Number 0 Point to 6 month 2 Number 0 All data between points . - A Previous data ;
for example : Data before January , Actually less than The current time is pushed forward by one month All data of the time node . - A Later data
for example : Data within a week , Is actually greater than Push forward the current time by one week All data of the time node .
How to implement the code ?
We mainly use $gt and $lt These two operations ,$gt Is greater than a certain value ,$lt Is less than a certain value .
The specific code is as follows :
Model.find({ createdAt: { $gt: startTime } }); // Realize the third situation
Model.find({ createdAt: { $lt: endTime } }); // Realize the second situation
Model.find({ $and: [{ createdAt: { $gt: startTime } }, { createdAt: { $lt: endTime } }] }); // Realize the first case
among createdAt yes mongoose The default timestamp field when storing data in the database ,createdAt It represents the time when the data is created .startTime Indicates the start time of the query condition ,endTime It means the end time .$and It means with , It means that the following conditions are met at the same time .
边栏推荐
- 太空游戏第12课: 盾牌
- Shanghai Jiaotong University joined hands with Taobao to set up a media computing laboratory: promoting the development of key technologies such as video super score
- DOS common commands
- Linxu 【权限,粘滞位】
- Related concepts of multitasking programming
- [C language] use function pointers to make a different calculator
- Use of DDR3 (axi4) in Xilinx vivado (2) read write design
- Common commands of raspberry pie
- Network shell
- [fasttext -- Summary notes]
猜你喜欢

Install keras, tensorflow, and add the virtual environment to the Jupiter notebook
Database tuning - connection pool optimization

C language data 3 (1)

Wust-ctf2021-re school match WP
![[C语言刷题篇]链表运用讲解](/img/44/1de4e3f0712780680fbdc904dfe39a.png)
[C语言刷题篇]链表运用讲解
关于链接到其他页面的标题

83. (cesium home) how the cesium example works

一文让你搞懂什么是TypeScript

Related concepts of multitasking programming

Dsactf July re
随机推荐
Windows系统下Mysql数据库定时备份
The engineering practice of super large model was polished, and Baidu AI Cloud released the cloud native AI 2.0 solution
MySQL startup error 1607 unexpected process termination
js可拖拽alert弹窗插件
LeetCode_ Bit operation_ Medium_ 260. Number III that appears only once
类与对象(中)
Raspberry pie 4B parsing PWM
User and group and authority management
Common instructions of vim software
LeetCode-297-二叉树的序列化与反序列化
[POC - proof of concept]
Teach you how to draw a map with ArcGIS [thermal map]
太空射击第16课: 道具(Part 2)
Practice of real-time push demo of three web messages: long polling, iframe and SSE
Speech controlled robot based on ROS (I): realization of basic functions
About the title of linking to other pages
DOS common commands
3D激光SLAM:LeGO-LOAM论文解读---简介部分
树行表达方式
LeetCode_位运算_中等_260.只出现一次的数字 III