
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
MongoDB Query to search for records only in a specific hour?
For this, use the $hour operator. Let us first create a collection with documents with one of the field as date −
> db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry","OrderDatetime":new ISODate("2019-01-31 09:45:50")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6e8a86d78f205348bc62a") } > db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry","OrderDatetime":new ISODate("2019-02-21 01:10:01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6e8b86d78f205348bc62b") } > db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry","OrderDatetime":new ISODate("2019-04-01 04:10:11")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6e8e26d78f205348bc62c") } > db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry","OrderDatetime":new ISODate("2019-05-11 08:53:01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6e8f26d78f205348bc62d") }
Following is the query to display all documents from a collection with the help of find() method −
> db.mongoDbSearchForHoursDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd6e8a86d78f205348bc62a"), "CustomerName" : "Larry", "OrderDatetime" : ISODate("2019-01-31T09:45:50Z") } { "_id" : ObjectId("5cd6e8b86d78f205348bc62b"), "CustomerName" : "Larry", "OrderDatetime" : ISODate("2019-02-21T01:10:01Z") } { "_id" : ObjectId("5cd6e8e26d78f205348bc62c"), "CustomerName" : "Larry", "OrderDatetime" : ISODate("2019-04-01T04:10:11Z") } { "_id" : ObjectId("5cd6e8f26d78f205348bc62d"), "CustomerName" : "Larry", "OrderDatetime" : ISODate("2019-05-11T08:53:01Z") }
Following is the query to search for specific hours. Here, we will get records for specific hours 8 and 1. The output would display the ID for both the times (hours) −
>db.mongoDbSearchForHoursDemo.aggregate([{$project:{SpecificHours:{$hour:"$OrderDatetime"}}}, {$match:{SpecificHours:{"$in":[08,01]}}}]);
This will produce the following output −
{ "_id" : ObjectId("5cd6e8b86d78f205348bc62b"), "SpecificHours" : 1 } { "_id" : ObjectId("5cd6e8f26d78f205348bc62d"), "SpecificHours" : 8 }
- Related Articles
- MongoDB query to search date records using only Month and Day
- MongoDB query for specific case insensitive search
- MongoDB query to search for Month and Day only?
- Query MongoDB for a nested search
- MongoDB query to get only a specific number of elements
- How to run MongoDB query to update only a specific field value?
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query for ranking / search count?
- MongoDB query to group records and display a specific value with dot notation
- MongoDB query to update only a single item from voting (up and down) records?
- A single MySQL query to update only specific records in a range without updating the entire column
- MongoDB query select and display only a specific field from the document?
- MySQL query to conduct a basic search for a specific last name in a column
- MongoDB query to get date records in a range
- MySQL query for text search with LIKE and OR to fetch records

Advertisements