This is my user collection in mongodb
{
"_id" : NumberLong(104060),
"age" : 41,
"username" : "appubhai75#gmail.com",
"roles" : [
"ROLE_USER"
],
"firstName" : "Apurva",
"lastName" : "Shah",
"email" : "appubhai75#gmail.com",
"createdDate" : ISODate("2016-02-08T12:23:02.001Z"),
}
i am using criteria like this
criteria = new Criteria().orOperator(name, email)
.andOperator(genderCriteria).and("_id").ne(id).and("createdDate").gte(startDate).lte(endDate);
when i am passing startdate and enddate like:
Start date is::2015-12-16T00:00:00.000+05:30
end date is::2016-02-08T00:00:00.000+05:30
this user is not coming ,but when i change my enddate to
end date is::2016-02-09T00:00:00.000+05:30
then it is coming.
If i am using gte and lte then it must include the date equal to also .Like,when i pass the date from 16 dec to 8 feb, then it is giving all the users with created date 16 to 7 feb. not including the user with created date 8 feb,and when i am passing 16 dec to 9 feb then it include 8 feb user.Is anything am doing wrong i am confuse.Please help.
Thank you.
You won't get results for this
Start date is::2015-12-16T00:00:00.000+05:30
end date is::2016-02-08T00:00:00.000+05:30
Check the created Date for user it is ISODate("2016-02-08T12:23:02.001Z") date is equal but not the time.Overall your user date is not in your range .Mongo it checks for both date and time equality.
The one solution you can have is at your application side add additional time 24*60*60 - 1 to this Date createdDate using DateUtils in org.apache.commons.lang.time(Apache Commons Lang)
DateUtils.addSeconds(createdDate,24*60*60-1);
Important Assuming that you will send only date in request.You have to add above code to pre proprocess your request object but not when your inserting.When ever a request comes add this piece of code.It will work fine
Related
what i want is to get the users who have created there account in between the range of dates and also want to get the customer with order count greater then 1 for those new customers, so what i am doing is trying to get the data greater then the last month i.e 1st of march 2020 but the output is giving me users till 1 april, why not till today i.e 11 april, the data is in following format
"_id" : ObjectId("1dv47sd1a10048521sa1234d"),
"updatedAt" : ISODate("2020-04-01T16:19:26.460+05:30"),
"createdAt" : ISODate("2020-04-01T16:18:46.066+05:30"),
"email" : "edx#gmail.com",
"phone" : "xxxxxxxxxx",
"password" : "$awdad$2b$10$4YaO6AEZqXA9ba0iz14ALi",
"dob" : "00/20/1990",
"stripeID" : "xxxxxxxxxxxxxxxx",
"__t" : "Customer",
"banned" : false,
"picture" : "link to image",
"name" : {
"first" : "ababab",
"last" : "Saaa"
},
"orderCount" : 2,
"creditCards" : [ ],
"addresses" : [ ],
"__v" : 0,
"isEmailVerified" : true
i have written a query for extracting data from the date greater then last month but it is giving me data till the 1 of april, my query is as follows
db.users.find({
"createdAt" : { "$gte" :new Date("2020-03-1") }
})
so i want to get data timm today from 1 march 2020 also order count is greter then 1,thanks in advance i am preety new with mongo
MongoDB only stores absolute times (aka timestamps or datetimes), not dates. As such the queries should generally be specified using times, not dates, also.
Furthermore, MongoDB always stores times in UTC. Different systems can operate in different timezones. For example, the shell may be operating in a different timezone from your application.
To query using dates:
Determine what timezone the dates are in.
Convert dates to times in that timezone. For example, 2020-03-01 00:00:00 UTC - 2020-03-31 23:59:59 UTC and 2020-03-01 00:00:00 -0400 - 2020-03-31 23:59:59 -0400 are both "March" and would match different sets of documents.
Convert the times to UTC. This may be something that a framework or library you are using handles for you, for example Mongoid in Ruby does this.
Use the UTC times to construct MongoDB queries.
This is stated in the documentation here.
I have a document that has the following property amongst others
{
dueDate: ISODate("2019-12-29T13:00:00.000Z")
}
I'm building an aggregation, and in the $project stage, I am attempting the following:
"utcDueDate": "$dueDate",
"rawDueDate" : {
"$dateToString" : {
"date" : "$dueDate",
"timezone" : "Australia/Sydney"
}
},
"prettyDueDate" : {
"$dateToString" : {
"date" : "$dueDate",
"format" : "%d/%m/%G",
"timezone" : "Australia/Sydney"
}
}
I am expecting the following output:
utcDueDate: 2019-12-29T13:00:00.000Z
rawDueDate: 2019-12-30T00:00:00.000Z
prettyDueDate: 30/12/2019
However, I am getting 30/12/2020 for prettyDueDate.
If I use %Y instead of %G it works as expected.
%G is meant to be the ISO standard format for year (0000-9999). I am using mongo version 4.0.13
Can anyone please explain why this is happening? Quite a few aggregations that are used for reporting have this format string and I'd really like to avoid having to change them all
This is expected behavior for %G since it is using a 'week numbered year' calendar.
There is a very detailed explanation of this behavior here. I've copied a small piece of the explanation below:
The phrase "ISO 8601 format" in the documentation refers to a way of
specifying dates that breaks them up by week number and day number
instead of the traditional month and day. Years in this format always
begin on Monday, so on years where December 31 is not a Sunday, it
gets treated as being part of the next year by the ISO 8601 calendar.
I am new to Mongo DB. Can anyone help me in how to get the number of weekdays between two Given dates in month wise.
$dayOfWeek of a date will give the value of the date 1 for sunday and 7 for saturday.
But my question is how can i increment the date from start date to end date to find the number of weekdays.
Sample Data:
db.data.insert({"startDate":ISODate('2016-01-01'), "endDate":ISODate('2016-03-31')})
WriteResult({ "nInserted" : 1 })
> db.data.find().pretty()
{
"_id" : ObjectId("57c6e6a6a1e49d654caca17a"),
"startDate" : ISODate("2016-01-01T00:00:00Z"),
"endDate" : ISODate("2016-03-31T00:00:00Z")
}
Now need to find the number of Weekdays between the Start Date (2016-01-01) and End Date (2016-03-31).
With output giving Jan:21,Feb:21,Mar:23.
Please suggest for an solution...
I'm developing an app using a MongoDB database that needs to check for items enabled for today's particular weekday.
Items can be enabled for any individual days of the week. (eg: Monday and Wednesday, or Tuesday and Thursday and Saturday, every day, whatever)
I was going to do this:
var currentWeekDay = Math.pow(2,new Date().getDay());
Therefore
Sunday === 1
Monday === 2
Tuesday === 4
Wednesday === 8
...
Saturday === 64
An example item might be like this
{_id:'blah', weekDays:127}
Now I want to query all items that are enabled for today...
MongoDB has an operator $and, but that's only for logical operations.
It has $bitsAnySet, but it looks like it's only implemented in 3.16.
https://jira.mongodb.org/browse/SERVER-3518
I'm running MongoDB v2.6.10.
So I'm wondering how to come up with a sensible indexable query.
Maybe
{_id:'blah', w0:1, w1:1, w2:1, w3:1, w4:1, w5:1, w6:1} //every day
{_id:'blah', w0:1, w1:0, w2:0, w3:0, w4:0, w5:0, w6:1} //Sat and Sun
That would be easily indexable. Can anyone think of a more terse way of doing it?
One option would be storing days as an array of integers:
{ '_id' : '1' , 'weekDays' : [0,1,2,3,4] } // mon-fri
{ '_id' : '2' , 'weekDays' : [5,6] } // sat-sun
Then you could create a simple index on weekDays field:
db.collection.createIndex({ weekDays : 1 })
And querying would also be pretty simple:
db.collection.find({weekDays : 2}) // wed
I'm building an event website. There are 2 types of events:
Events with specific dates and times. For example, a theatre show can have a show Jan 10 at 8pm, Jan 11 at 8pm and Jan 13 at 7pm.
Events which open a range of hours during several days. For example, an exhibition on a museum can open from Jan 10 to Jan 30 from 10am to 6pm.
I need to save the dates and times so that I can answer the following questions/queries:
Which events are going to happen tomorrow from 7pm to 12am?
Which events are going to happen this weekend?
Which events are about to finish? (the last day is less than one week away)
If we didn't have events type 2, we could have the following squema:
name
category
dates: an array of dates (each day would be on the array)
But because we have events type 2, it has to be different. I thought on having:
name
category
dates: an array of objects like {"2015-01-10 09:00": "2015-01-10 18:00"} with the range of hours of each day.
But I think it's not possible to do a query to solve Question 1 with this squema. Am I wrong?
How would you structure the data so I could answer those three questions?
thanks!
It was easier than I tought.
First, on MongoDB you can't have dates as keys.
The model is:
{
"name" : "Bob Dylan",
"category" : "Exhibition",
"dates" : [
{
"init" : ISODate("2015-01-08T08:00:00Z"),
"end" : ISODate("2015-01-08T19:00:00Z")
},
{
"init" : ISODate("2015-01-09T08:00:00Z"),
"end" : ISODate("2015-01-09T21:00:00Z")
},
{
"init" : ISODate("2015-01-10T08:00:00Z"),
"end" : ISODate("2015-01-10T21:00:00Z")
}
],
"createdAt" : ISODate("2015-01-09T16:33:51.338Z")
}
And the query is:
return Events.find({
'dates.init' : { $gte: dateInit },
'dates.end' : { $lte: dateEndPlusOneDay }
});