java mongodb search on a given date - java

We have a user_audit entity(table) in mongodb which stores all the login/logout information.
For example,
"_id" : ObjectId("5228cf0156961de6693b74c0"),
"active" : true,
"fname" : "Tom",
"lastlogin" : ISODate("2013-09-05T18:35:45.608Z"),
"lastloginip" : "0:0:0:0:0:0:0:1",
"lname" : "Bailey",
"lastlogout" : ISODate("2013-09-05T18:36:45.568Z"),
There are thousands of records in this table in production.
Now, the admin wants to look for all the logins on a particular date. i am not able to look for exact match of date because of the "time" information attached to ISODate in the "lastlogin" field. In Java, new Date() had been used to insert this field value.
The issue is the time information keeps changing for logins on a particular day.
Tried,
query.put("lastlogin", new BasicDBObject("$lte", givenDate).append("$gte", givenDate));
Obviously it does not return results because the time part of the date does not match.
The query passed from Java to mongo is:
query={ "lastlogin" : { "$lte" : { "$date" : "2013-09-05T04:00:00.000Z"} , "$gte" : {
"$date" : "2013-09-05T04:00:00.000Z"}}}
[Note: It defaults to 04:00:00.000Z always if we format with MM_dd_yyyy in java, not sure why..]
The issue is we have a lot of records in production database. For fresh records, i can blank out the time portion before inserting from Java. But for existing records, not sure how to handle it. How can i get the records on a given date?

According to mongodb cookbook, you are in the right direction. You may just query for something like
query.put("lastlogin", new BasicDBObject("$lt", givenDatePlusOneDay).append("$gte", givenDateAt00h00min));
which is an interval and not the same date.

Related

How do I convert String to an Object in MongoDB?

In my Mongo database, I have a "Books" collection with a "category" field. In the past, "category" was an enum mapped as String in Mongo but now I need "category" to be an object within my "Books" collection. What can I do? Is there a query that can be executed to convert all documents at once?
Example, today category is like that:
"category" : "Sci-Fi and Fantasy"
But I need to convert to this:
"category" : {
"_id" : ObjectId("3f07bc56po324021df23a8f1"),
"code" : NumberLong(1),
"name" : "Sci-Fi and Fantasy"
}
MongoDB is no SQL you can have both kind of records present in your mongo collection, although while reading the records you might have to frame some logic for creating identical entities. This might cause issues if you search based on that particular property.
You can create a new property with different name keeping the category as well and then add the new property to all the records.

Querying by date in mongo db

I am new to Mongodb. I want to find those objects stored in mongodb whose receivedOn date is greater than a particular date. My object structure is :
{
"_id" : ObjectId("591313fa79a7f2826cdfcdbd"),
"uuid" : "849cf178-bf19-4a32-bda8-b754551c57f0",
"status" : "SENT",
"receivedOn" : ISODate("2017-05-10T18:51:58.893+05:30"),
"scheduledOn" : ISODate("2017-05-10T18:51:58.893+05:30"),
"deliveredOn" : ISODate("2017-05-10T18:52:02.628+05:30")
}
I am using Jongo driver for querying. My query looks like:
collection.find("{\"receivedOn\": {$gte : #}}", javaDate);
I am getting no results from this query but there are documents in collection which should have been returned. What's wrong with my query?
db.CollectionName.find({"receivedOn" : new ISODate("2017-05-10T18:51:58.893+05:30") });
As you are using java,util.Date, there could be timezone issues when Date object is converted to json. Java might be adding/subtracting hours to your date based on timezone. Ideally while storing dates, they should be in UTC format, and while retrieving, they should be in UTC.

Query by date in mongodb using java

I was going to get distinct values form collection.
I stored time as follows:
"time" : ISODate("2017-01-26T09:46:26.523Z")
new ISO8601DateFormat() is not working, that gives me below error
org.bson.codecs.configuration.CodecConfigurationException: Can't find
a codec for class
com.fasterxml.jackson.databind.util.ISO8601DateFormat.
My code is looks like below.
Query query = new Query();
query.addCriteria(Criteria.where("user_id").is(id).and("time").gt(new ISO8601DateFormat()));
mongoTemplate.getCollection("user_log").distinct("timezone", query.getQueryObject())
My mongodb terminal command is follows and it works perfectly.
db.user_log.find({ "user_id" : "1" , "time" : { "$gt" : new ISODate("2017-01-25T00:16:15.184Z")}})
What is correct way to approach when I access from java?
Instant instant = Instant.parse("2017-01-25T00:16:15.184Z");
Date time = Date.from(instant);
Replace your time criteria with below
and("time").gt(time)

Load a big json file in Mysql or Oracle database

At work, we supply files for other services. Size of them are between 5mo and 500mo.
We want to use Json instead of XML, but i m wondering how our customers could
upload those files in an easy way in their database, Oracle or Mysql.
I mean, i can t find on the web APi or functions or tools, in Mysql or oracle, to do that.
I know that it s easy to work data by data to load a small Json file, decode each
object or array and put them at the right place in database.
But is there an other way to do this, like sqlloader in Oracle ?
And if so, size of our file aren t they too big to produce JSON file, in JAVA for example ?
I guess it might be difficult to do this load job automatically, especially because of arrays like this :
{"employees":[
{"firstName":"John", "lastName":"Doe", "salaryHistory":[1000,2000,3000]},
{"firstName":"Anna", "lastName":"Smith", "salaryHistory":[500,800]},
{"firstName":"Peter", "lastName":"Jones", "salaryHistory":[400]}
]}
where salaryHistory must produce problems because their sizes are different, and data are not madatoryly
in the same table.
Any ideas or help would be welcomed !
Edit
i m looking for a solution to put each data in the good column of a table, i don t need to store a Json structure in a single column of simple table.
like this :
table employees : column are id, FirstName, lastName and
table salaryHistory : column are id, order, salary
and each data must go in the good column like "John" in firstname, "Doe" in lastname, then "1000" in a new row of table salaryHistory , "2000" in another new row of salaryHistory and so on.
Starting with MySQL 5.7 there is a new data type: JSON.
Take a look here for more details.
Example for Oracle 12c:
create table transactions (
id number not null primary key,
trans_msg clob,
constraint
check_json check (trans_msg is json)
);
regular insert:
insert into transactions
values
(
sys_guid(),
systimestamp,
'{
"TransId" : 3,
"TransDate" : "01-JAN-2015",
"TransTime" : "10:05:00",
"TransType" : "Deposit",
"AccountNumber" : 125,
"AccountName" : "Smith, Jane",
"TransAmount" : 300.00,
"Location" : "website",
"CashierId" : null,
"ATMDetails" : null,
"WebDetails" : {
"URL" : "www.proligence.com/acme/dep.htm"
},
"Source" : "Transfer",
"TransferDetails" :
{
"FromBankRouting" : "012345678",
"FromAccountNo" : "1234567890",
"FromAccountType" : "Checking"
}
}'
)
/
SQL*Loader control file and data file:
load data into table transactions
fields terminated by ','
(
trans_id sequence(max,1),
fname filler char(80),
trans_body lobfile(fname) terminated by EOF
)

Difference between _id & $oid ; $date & IsoDate in mongo database

We are using mongo db to store certain records in production database.
We see our records having "_id" : { "$oid" : "50585fbcb046b2709a534502"} in production database , while we see same record as "_id" : ObjectId(" 50585fbcb046b2709a534502 ") in the qa database.
For dates we see "ld" : { "$date" : "2011-12-03T17:00:00Z"} in prod database, while "ld" :ISODate("2011-12-03T17:00:00Z") in qa database.
We have tested our queries successfully in qa environment, but worried it might fail in production
1) Will my java queries work seamlessly on prod & qa both? (I am using morphia apis to query)
2) Are they internally being stored in the same identical way?
To answer the two questions:
Yes they will
Yes they are the same, it is merely the representation within the item you are looking in (console or app) as to how they display. Console (later versions anyway, about 1.4+) will display ObjectId and ISODate (normally) whereas picking it out directly from the server language (Java in your case) will tend to show the full objects properties ($oid and $date in this case).

Categories