0 votes
1 view
in Web Technology by (47.8k points)

I've been playing around storing tweets inside mongodb, each object looks like this:

"_id" : ObjectId("4c02c58de500fe1be1000005"), 

"contributors" : null, 

"text" : "Hello world", 

"user" : { 

"following" : null,

"followers_count" : 5, 

"utc_offset" : null, 

"location" : "", 

"profile_text_color" : "000000", 

"friends_count" : 11, 

"profile_link_color" : "0000ff", 

"verified" : false, 

"protected" : false, 

"url" : null, 

"contributors_enabled" : false, 

"created_at" : "Sun May 30 18:47:06 +0000 2010", 

"geo_enabled" : false, 

"profile_sidebar_border_color" : "87bc44", 

"statuses_count" : 13, 

"favourites_count" : 0, 

"description" : "", 

"notifications" : null, 

"profile_background_tile" : false, 

"lang" : "en",

"id" : 149978111, 

"time_zone" : null, 

"profile_sidebar_fill_color" : "e0ff92" 

}, 

"geo" : null, 

"coordinates" : null, 

"in_reply_to_user_id" : 149183152, 

"place" : null, 

"created_at" : "Sun May 30 20:07:35 +0000 2010", 

"source" : "web", 

"in_reply_to_status_id" : { 

"floatApprox" : 15061797850 

}, 

"truncated" : false, 

"favorited" : false, 

"id" : { "floatApprox" : 15061838001 }

How would I write a query which checks the created_at and finds all objects between 18:47 and 19:00? Do I need to update my documents so the dates are stored in a specific format?

2 Answers

0 votes
by (107k points)

To find the objects between two dates MongoDB you can use below-mentioned code to find the record between two dates using $gte and $lt:-

db.CollectionName.find({"whenCreated": { 

'$gte': ISODate("2018-03-06T13:10:40.294Z"), 

'$lt': ISODate("2018-05-06T13:10:40.294Z") 

}

});

0 votes
by (108k points)
edited by

You have to keep the following points in your mind while working with the date. 

1. You basically have to pass a Javascript Date object

2. The object has to be ISODate friendly 

3. Manipulate the data to ISO

Refer the following snippet:

var input_date = new Date(myDate.toISOString());

MyModel.find({'date': { $lte: input_date }})

Related questions

0 votes
1 answer
asked Jul 15, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 9, 2019 in SQL by Sammy (47.8k points)
0 votes
1 answer
asked Jul 17, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...