I want to add a date field in mongo db. I am using robo mongo. How can i do that?
If it is a string we can do it by "", if it is a number we can do it by NumberInt. What is the datatype for date field? Also, how can I insert current date?
db.yourcollection.insert({date_field_name:new Date()})
Related
I would like to insert the following document in mongodb:
Query query = new Query();
query.addCriteria(Criteria.where("client").is(client));
Update update = new Update();
update.set("dateTime", new DateTime());
update.set("maxDate", new DateTime("9999-12-31T00:00:00.000+01:00"));
mongoTemplate.upsert(query, update, MyClass.class);
and the inserted object is
{
"client" : "client",
"dateTime" : ISODate("2020-03-12T10:11:35.077Z"),
"maxDate" : Date(253402210800000)
}
I would like the maxDate to be in ISODate format. Is it possible ?
Is 9999-12-31T00:00:00.000+01:00 too far for mongodb?
Is there any issue with such a great value?
I read here https://docs.mongodb.com/manual/core/shell-types/ that mongoDB could support year 9999
EDIT with datagrip instead of robomongo usage
if I use datagrip to read the documents instead of robomongo, the format is correct. Could the issue come from robomongo display?
I haven’t tried or run this. But I believe using a different constructor for DateTime should resolve this.. please try below constructor for maxDate field..
public DateTime(int year,
int monthOfYear,
int dayOfMonth,
int hourOfDay,
int minuteOfHour,
int secondOfMinute,
int millisOfSecond,
DateTimeZone zone)
We are trying to insert a document with the current date as it's field. We are writing in java using eclipse plugin for mongodb. We want to execute the Date() command of mongo to get the date from mongo and not from java.
How can I execute this mongo query?
db.example.insert({"date":new Date()})
I found this question in a previews question but the answer was not helpful
Link
The standard driver takes java.util.date types and serializes as BSON dates. So with a collection object to "example"
Date now = new Date();
BasicDBObject timeNow = new BasicDBObject("date", now);
example.insert(timeNow);
If you are looking for a way to use the "server" time in operations, there is the $currentDate operator, but this works with "updates", so you would want an "upsert" operation:
BasicDBObject query = new BasicDBObect();
BasicDBObject update = new BasicDBObject("$currentDate",
new BasicDBObject("date", true)
);
example.update(query,update,true,false);
Since that actually is an update statement, you need to be careful that you are not actually matching any documents if you intend this to be an insert only. So it would be best to make sure your "query" contains unique information, such as a newly generated _id or something equally unique.
You can do it trying something like this:
db.example.insert({"date":ISODate("2016-03-03T08:00:00.000")});
Use this:
db.example.insert({"date":new Date(Date.now())});
There is a key difference I noted when using Date() as follows.
{ dateWhenCreated : Date() }
vs
{ dateWhenCreated : new Date() }
Notice the "new" keyword in the second usage. Whereas the first usage loads the data "as a string", the second one loads date as a field with date data type.
This might impact your sorting capability - dates stored as strings don't get sorted the same way as dates stored as dates.
Per the mongodb documentation here
I am working with Mongodb and I need to retrieve set of records with range of specified dates,like from date to to date.
I am using criteria object from java to form a query
But it not considering the equals date
ex:
fromDate:15-09-2017
tdate:23-09-2017
in to date it is not considering 23-09-2017 since it is lte(less than equals) it should take it and fetch the records.
Query query = new Query(Criteria.where("b").elemMatch(Criteria.where("startDate").lte(date).and("endDate").gte(date) )
Can anyone help me on this?
I'm writing the below query to get records between two dates. I'm using Mysql version 5.5. May its duplicate exactly I didn't know. But no answer working for me so that I'm asking. I'm following least date after latest date. Even though its not working.
Problem: Empty resultset.
pstmt=con.prepareStatement("SELECT urlid FROM youtubevideos WHERE lastwatched >=? AND lastwatched <=? order by id desc LIMIT 8");
pstmt.setString(1,previousdate);//14-05-2015
pstmt.setString(2,currentdate);//12-08-2015
rs=pstmt.executeQuery();
while(rs.next())
{
.........
}
But I'm getting empty resultset.
My table youtubevideos contains records
urlid lastwatched
-------------------
url1 12-08-2015
url2 11-08-2015
url3 08-05-2015
url4
url5 10-08-2015
Above is some data. Here lastwatched is of varchar and lastwatched is empty for some records. If my previous date 08-05-2015 means less than the current day (12) then above query working. Otherwise (from 13-05-2015 onwards) its not working. Any suggestions please.
You are using wrong date format for sql:
12-08-2015 // this is the output format
use yyyy-MM-dd instead:
2015-08-12 // this is the sql store format
This query works great in my Mysql database:
SELECT * FROM your_table where DATE <= "2015-05-08" AND DATE >= "2015-08-12"
To convert your strings:
Date initDate = new SimpleDateFormat("dd-MM-yyyy").parse(date);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String parsedDate = formatter.format(initDate);
Here lastwatched is of varchar
The issue is that you are storing date fields as type VARCHAR. This would work if your date format was Y-m-d since sorting this alphabetically is the same as sorting by date.
I recommend you change the lastwatched column to be a date type, this will allow the BETWEEN to work correctly and will also provide access to the date functions in MySQL.
Fix the data in the table. You should not be storing bona fide dates as varchar(). MySQL has a great data type for them, called date (or perhaps datetime.
Here is one method:
alter table youtubevideos add column NEW_lastwatched date;
update youtubevideos
set NEW_lastwatched = str_to_date(lastwatched, '%d-%m-%Y');
alter table drop column lastwatched;
alter table rename column NEW_lastwatched lastwatched date;
Then, pass in your parameters in the ISO standard format 'YYYY-MM-DD' and your problems with dates using this column will be fixed.
i have table manager where i have columns like date , phonenumber, user.
i want to retrieve records like say i want the phonenumbers from the table whos date lies between 01-01-2014 to 31-01-2014.The operation is like everyday 50 to 100 numbers will be inserted into the table i would like to view the numbers inserted in a particular week or particular date interval.
i am using postgres ans my date column is a varchar type. i am trying to do this in a java web application and the date will be picked using a jquery datepicker.
Sample Data
Date Number user
01-01-2014 244548845 user1
02-01-2014 545454454 user2
05-01-2014 244540045 user1
10-01-2014 244540045 user1
20-01-2014 244540000 user2
when i select 01-01-2014 to 06-01-2014 i would like to fetch first 3 records
SET DATESTYLE = "DMY, DMY";
SELECT
*
FROM
xxx
WHERE
Date::date BETWEEN '01-01-2014' AND '06-01-2014'
ORDER BY
Date::date
LIMIT
3;
Also, mind using the type date for storing dates, and not varchar!
While creating a table the column should be mentioned as date(have to make sure column name is NOT DATE)
ex : create table abc(dataentrydate Date not null);
and for retrieving the records based on date this below query can be used
SELECT * FROM abc WHERE dateentrydate BETWEEN '17-01-2014' AND '22-01-2014' ORDER BY dateentrydate;
Thanks a lot for the response and help provided.