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.
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.
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)
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
)
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).