I'm new at Android dev, so I'm using ActiveAndroid to manage the DB and I'm having troubles using a query with a Date variable, I have this:
From fromQuery = new Select().from(Shift.class);
//It doesn't matter what currentShiftDates do.
CurrentShiftDates currentShiftDates = new CurrentShiftDates();
fromQuery = fromQuery.where("startDate > ? and startDate < ?",currentShiftDates.startDate,
currentShiftDates.endDate);
return fromQuery.execute();
I had read that the query needs to pass the parameter as a long value, so I've tried with .getTime():
fromQuery = fromQuery.where("startDate > ? and startDate < ?",currentShiftDates.startDate.getTime(), currentShiftDates.endDate.getTime());
But it still don't work.
What I'm doing wrong?
I' m very sorry, it wasn't working because i was writing "startDate" and the column name is "StartDate", so the correct query was:
fromQuery = fromQuery.where("StartDate > ? and StartDate < ?",currentShiftDates.startDate.getTime(), currentShiftDates.endDate.getTime());
Related
Im new on Groovy and need some help.
I have that table called RANGE with :
ID_RANGE - Integer
Year - Integer
six_Month - Integer
Initial_Range - String
Final_Range - String
Last_Update - TimeStamp
I have to do a script to update/insert Initial_Range and Final_Range.
I will receive the new data from a map called "df_map" with the following data:
df_map.Update_date -> String
df_map.six_Month -> Integer
df_map.Initial_range -> String (Format "AA-123456678")
df_map.Final_range -> String (Format "AA-123456678")
That script have to validate some requirements,
if it doesn't, it can´t continues:
the date have to be a valid timeStamp (yyyy-MM-dd HH:mm:ss.SSS)
there must be 1000 values between Initial_Range and Final_Range
Only can update or insert a future date
When check all this, have to search if the register to modify exits, if not exits, have to create it.
that script has helpers to do 2 actions:
sqlQuery -
Make a query against the bbdd and show and array with the data. It get parameters like bbdd,query and parameters maps.
EXAMPLE:
query = "SELECT * FROM RANGE WHERE ID_RANGE = :RANGE"
params = [:]
params.RANGE = 1
outcome = sqlQuery(bbdd,query,params)
-sqlUpdate -
It make an insert or an update against the database and returns an array with the result. It receives as parameters the database, query, parameter map.
EXAMPLE:
query = "UPDATE RANGE SET Initial_Range = :Initial_Range WHERE ID_RANGE = :RANGE"
params = [:]
params.RANGE = 1
outcome = sqlUpdate(bbdd,query,params)
I work with sql but never before with groovy.....
Thanks in advance ;)
I tried to click on from-date textbox to select the From-Date but I am not able to do so for "opensource-demo.orangehrmlive.com". Dashboard > Apply leave
driver.findElement(By.id("applyleave_txtFromDate").click();
Select secMonth = new Select(driver.findElement(By.xpath("//div[#class='ui-datepicktitle']/select[1]")));
secMonth.selectByVisibleText("Jan"); Select secYear = new Select(driver.findElement(By.xpath("//div[#class='ui-datepicker-title']/select[2]")));
secYear.selectByVisibleText("2021");
java.util.List<WebElement> dates = driver.findElements(By.xpath("//td[#data-handler='selectDay']"));
int count = dates.size();
for(int i=0;i<count;i++)
{
String ReqD = dates.get(i).getText();
if(ReqD.equalsIgnoreCase("2"))
{
dates.get(i).click();
break;
}
it does not work?
String someDate = "2020-01-02";
WebElement fromDate = driver.findElement(By.id("applyleave_txtFromDate");
fromDate.click();
fromDate.sendKeys(someDate);
fromDate.sendKeys(Keys.TAB);
I would recommend for you to use AppConstants to add all final values to it. Then it will be easier to pass it using String
(example: public static final String DATE_VALUE = "12/22/2020";)
I use this approach for automation testing, because of you are going to use this script for long run as a regression testing for your framework, it will not pass when the calendar month will change. You will have to update your script. But if you will pass it within String value, your script will not need modifications.
I already can execute the desired query on mongoshell, but i need to make the same query using Java and MongoOperations.
I have checked this question, which is very similar, but it only has one condition, as mine has two and uses the $gte and $lt operators. Here's the working mongo Query:
db.getCollection('example').update({"idVar": "desiredValued"}, { $pull: { "listaHoras": { $gte: ISODate("2016-11-06T05:50:00.000Z"), $lt: ISODate("2016-11-06T06:30:00.000Z")}}})
Sample doc:
"_id" : ObjectId("58221b4610a3c71f1894ce75"),
"idVar" : "56b11259272f5515b05d70bc",
"date" : ISODate("2016-11-06T03:00:00.000Z"),
"listaHoras" : [
ISODate("2016-11-06T05:40:00.000Z"),
ISODate("2016-11-06T06:30:00.000Z"),
ISODate("2016-11-06T06:40:00.000Z")
]
Where i'll have the ISODATE as a Date variable in Java, and the desiredValue as a String variable.
So far, i have i did the following, using the previously mentioned question as example:
BasicDBObject match = new BasicDBObject("idVar", desiredValue); // to match your document
BasicDBObject update = new BasicDBObject("listaHoras", new BasicDBObject("itemID", "1"));
coll.update(match, new BasicDBObject("$pull", update));
But, as you can see, this is NOT equivalent to the desired query. Since the match for the $pull is matching "itemID"with "1". I do not know, nor was i able to find how to properly use the $gte and $lt on the same query. Neither on how to use just one or both of them. I know it CAN be done as seen on the MongoOperatioons API which says:
"update - the update document that contains the updated object or $ operators to manipulate the existing object."
Anyone knows how it can be done? And if the Date type in Java matches the ISODATE on the Mongo?
I managed to find a solution. It is similar to what Veeram posted as a answer but it's slightly different. I simply removed his updateCriteria and used a BasicDBObject in it's place.
Here's how the full code looks:
Query findQuery = new Query();
Criteria findCriteria = Criteria.where("idVar").is(idVar);
findQuery.addCriteria(findCriteria);
Update update = new Update().pull("listaHoras", new BasicDBObject( "$gte", start).append("$lte", end));
mongoOps.updateMulti(findQuery, update, "CollectionName");
Where start and end are Date variables recieved by the method. Also important to note that Mongo uses the UTC as default timezone, so we must properly format the time in order for it to remove the desired values.
You can try something like below. This will remove the two items from the listaHoras array.
Query findQuery = new Query();
Criteria findCriteria =
Criteria.where("idVar").is("56b11259272f5515b05d70bc");
findQuery.addCriteria(findCriteria);
LocalDate startDt = LocalDate.of(2016, 11, 6);
LocalTime startTm = LocalTime.of(5, 40, 0);
LocalDate endDt = LocalDate.of(2016, 11, 6);
LocalTime endTm = LocalTime.of(6, 35, 0);
Date start = Date.from(LocalDateTime.of(startDt, startTm).toInstant(ZoneOffset.UTC));
Date end = Date.from(LocalDateTime.of(endDt, endTm).toInstant(ZoneOffset.UTC));
Query updateQuery = new Query();
Criteria updateCriteria =
Criteria.where(null).gte(start).lt(end);
updateQuery.addCriteria(updateCriteria);
mongoOperations.updateMulti(findQuery, update, "example");
So, i am making this program, trying to learn more about Spring and MongoDb. I have built this ticked module, that uses a MongoDb to store ticket info in a JSON format. It looks something like this:
> {
> "_id" : ObjectId("581fb1a24beb291d27f95a50"),
> "userID" : "581ddccb4beb29112a7b4f77",
> "ticketStatus" : "Processing",
> "ticketSolution" : "Not_Solved",
> "ticketComment" : null;
> }
My question is how do i insert a comment into the "ticketComment" field?
(I would need something that uses Criteria.where("ticketID").is(ticketID))
With simplest details use :-
Criteria.where("ticketID").is(ticketID));
Query query = new Query(criteria);
BasicDBObject newValues = new BasicDBObject(columnName,value);
BasicDBObject set = new BasicDBObject("$set", newValues);
Update update = new BasicUpdate(set);
mongoOperations.updateMulti(query, update, "collectionName")
So, after some messing around, i have found another approach to this problem.
#Override
public void addTicketComment(String ticketID, String ticketComment) {
Ticket ticket = mongoTemplate.findById(ticketID, Ticket.class);
ticket.getTicketComments().add(ticketComment);
mongoTemplate.save(ticket);
}
I have a list of files in a GridFS that I am attempting to query by date. The sample document looks like the following:
{
"_id" : ObjectId("52e431d3e84f6fa18c53c808"),
"chunkSize" : NumberLong(262144),
"length" : NumberLong(13021),
"md5" : "0eb01f0d266f4bf4764d4ffc7e70a7ed",
"filename" : "120_1390686674383",
"contentType" : null,
"uploadDate" : ISODate("2014-01-25T21:51:15.049Z"),
"aliases" : null
}
I am attempting to get the "most recent" according to timestamp by doing the following:
DateTime dt = new DateTime(queryObj.getTime()); //org.joda.DateTime
BasicDBObject sort = new BasicDBObject();
sort.put("uploadDate", -1);
BasicDBObject query = new BasicDBObject();
query.put("uploadDate", new BasicDBObject("$gte", dt));
DBCursor cursor = fileStore.getFileList(query, sort);
If I simply sort the fileStore I get numerous records back and I can enumerate via the cursor. However, whenever I try to use $gte or $lte I get zero results.
Is there a missing step?
You're passing in a joda DateTime reference which the driver doesn't know how to do. If you check the logs, you'll probably find that it essentially calls a toString() on that and passes that in. Since a string is not a Date, the comparison fails. Try passing in just a java.util.Date (you can get one from the DateTime, iirc).