mongodb关于时间date的查询——Querying for a Date Range
2012-05-22 14:52
441 查看
来源: http://cookbook.mongodb.org/patterns/date_range/
注意: javascript 中 ,月份是从0开始算起的。 如下面的例子中,
start表示 2010-04-01日。
Credit: Mike Dirolf
You want to list all of the documents in a collection (in the example we'll use "posts") that were created in a particular month. Each document in the collection has a field representing the date it was created:
We want to perform a query to get all documents whose value for
Use a range query to query for documents whose value for
Our first step is to construct Date instances that we can use to do the range query. In JavaScript:
Note that in JS the month portion of the Date constructor is 0-indexed, so the
The logic here is similar in all languages, in Python we'd do:
Now that we have our reference dates, we can perform a range query to get the matching documents, note the use of the special
and
Again, this translates nicely to other languages - in Python it's:
To make these queries fast we can use an index on the
We can also use a compound index if we're performing a query on author and a date range, like so:
注意: javascript 中 ,月份是从0开始算起的。 如下面的例子中,
start表示 2010-04-01日。
var start = new Date(2010, 3, 1);
Querying for a Date Range (Specific Month or Day)
Credit: Mike Dirolf
Problem
You want to list all of the documents in a collection (in the example we'll use "posts") that were created in a particular month. Each document in the collection has a field representing the date it was created:{ "title" : "A blog post", "author" : "Mike", "content" : "...", "created_on" : new Date(); }
We want to perform a query to get all documents whose value for
created_onis in the month of April, 2010.
Solution
Use a range query to query for documents whose value for created_onis greater than a Date representing the start of the month, and less than a Date representing the end.
1. Construct Date objects representing the start and end of the month
Our first step is to construct Date instances that we can use to do the range query. In JavaScript:var start = new Date(2010, 3, 1); var end = new Date(2010, 4, 1);
Note that in JS the month portion of the Date constructor is 0-indexed, so the
startvariable above is April 1st and the
endvariable is May 1st.
The logic here is similar in all languages, in Python we'd do:
>>> from datetime import datetime >>> start = datetime(2010, 4, 1) >>> end = datetime(2010, 5, 1)
2. Perform a range query
Now that we have our reference dates, we can perform a range query to get the matching documents, note the use of the special $operators,
$gte(greater-than)
and
$lt(less-than):
db.posts.find({created_on: {$gte: start, $lt: end}});
Again, this translates nicely to other languages - in Python it's:
>>> db.posts.find({"created_on": {"$gte": start, "$lt": end}})
3. Use an index for performance
To make these queries fast we can use an index on the created_onfield:
db.posts.ensureIndex({created_on: 1});
We can also use a compound index if we're performing a query on author and a date range, like so:
db.posts.ensureIndex({author: 1, created_on: 1}); db.posts.find({author: "Mike", created_on: {$gt: start, $lt: end}});
相关文章推荐
- 关于时间的所有说明: Date DateComponents Calendar DateFormatter IOS Swift 3.0
- 关于ArcGIS For Flex查询并返回结果
- vue2.x 时间范围 date range timepicker。只在项目中使用elementUI的date-picker
- Flex DateFormatter 时间格式化
- Swift4中对于日期时间的处理(Date、DateComponents、Calendar、DateFormatter、Locale)
- 关于时间的模糊查询
- 关于时间模糊查询的三种方法
- JpaRepository关于日期(不带时间)查询注意事项
- 关于SimpleDateFormat安全的时间格式化线程安全问题
- 关于查询动态时间的MDX语句
- 关于SQL时间类型的模糊查询
- 关于实体为Date类型的字段,如何用model.find(" date>? ",param)方法进行查询?
- SQL DATEDIFF语法及时间函数 Sql 查询当天、本周、本月记录
- 时间格式及关于时间的操作(SimpleDateFormat)
- hibernate 关于date类型的模糊查询解决方法
- 关于windebug查询进程中各个线程占用cpu时间,解决CPU占用很大问题
- oracle 按时间查询(to_date(),to_char()的巧用)
- SQL DATEDIFF语法及时间函数 Sql 查询当天、本周、本月记录
- Go语言技巧:使用for range time.Tick()固定间隔时间执行
- iOS dateformatter设置GMT格式时间--iOS开发系列---项目中成长的知识四