Hi I want to query MONGO using java.
I have a query like below:
db.flights.find({"timestamp" : {"$lte": new Date("2014-09-05T00:00:00.001Z")}}).count()
Which gives output as 68
When I try to get using java code like below:
table = db.getCollection("flights");
DBObject match = new BasicDBObject();
Date endDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse("2014-09-05T00:00:00.001Z");
match.put("timestamp", new BasicDBObject("$lte", endDate));
int count=table.find(match).count();
System.out.println(collections+" "+"COUNT : "+count);
The output is 48.
Can anybody tell me the issue in code ??
saving format
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
className classObject=null;
Date d1=new Date();
classObject.setDateColumn(format.parse(format.format(d1)));
/---------------------------------------------------/
checking format
Date d1=new Date();
Date startDate=format.parse((formatDate.format(d1)+" 00:00:00"));
Date endDate=format.parse((formatDate.format(d1)+" 23:59:59"));
classObject=classNameService.classObjectCount(startDate,endDate);
/---------------------------------------------------/
service method
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
#Autowired(required=true)
private MongoTemplate mongoTemplate;
className classObject=null;
Query query = new Query();
query.addCriteria(org.springframework.data.mongodb.core.query.Criteria.where("testId").in(testId));
query.addCriteria(org.springframework.data.mongodb.core.query.Criteria.where("testDate").gte(startDate).lte(endDate));
List<className> classNames=mongoTemplate.find(query, className.class);//put your class name..className
if(classNames.size()>0)
classObject=classNames.get(0);
Related
MongoCollection<Document> Person_List = db.getCollection("Person");
MongoCollection<Document> active_list = db.getCollection("activelist");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD");
String todaydate = format.format(new Date());
ArrayList<Document>
activeList=Person_List.find(Filters.regex("LastUpdate",todayDate.toString())).into(new
ArrayList<Document>());
This is the code what we have written, here we need to fetch the month from todaydate and need to compare with month of LastUpdate field, how to fetch the month from LastUpdate document field in mongo db, LastUpdate is the date string, can someone please help me.
I am trying to find all rows before a certain date using mongoTemplate in Spring Java. This is returning no rows though there are lots of old records.
What is the problem?
Calendar cal = GregorianCalendar.getInstance();
cal.add( Calendar.DAY_OF_YEAR, -73);
Date prevDate = cal.getTime();
List<AuditTrailDTO> dt = mongoTemplate.find(Query.query(Criteria.where("AUDIT_CREATE_DATETIME").lte(prevDate)), AuditTrailDTO.class);
AUDIT_CREATE_DATETIME in Mongo DB is stored in this format:
Audit_Create_DateTime:2019-07-10 08:47:02.078
It should work if the type of Audit_Create_DateTime is Date, so I assume Audit_Create_DateTime is of String type.
You can do the following to do a less-than-equals logic on a String field;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Query query = Query.query(Criteria.where("AUDIT_CREATE_DATETIME").lte(dateFormat.format(prevDate)));
List<AuditTrailDTO> dt = mongoTemplate.find(query, Customer.class);
which will return proper results, though I recommend properly storing Date values as correct type rather than in String type.
I'm trying to build a query for all orders which were created today.
My Order-Entity has a datetime field like this:
#Column(name = "OrderCreationDate")
#Temporal(TemporalType.TIMESTAMP)
private Date orderCreationDate;
Named-Query:
#NamedQuery(name = "OrderHeader.findByOrderCreationDate", query = "SELECT o FROM OrderHeader o WHERE o.orderCreationDate = :orderCreationDate")
I tried to build the query like this:
public List<OrderHeader> findFromToday() {
Date dateToday = new Date();
TypedQuery<OrderHeader> query = em.createNamedQuery("OrderHeader.findByOrderCreationDate", OrderHeader.class).setParameter("orderCreationDate", dateToday);
return query.getResultList();
}
Of course the ResultList is empty since the date AND time would have to match.
Unfortunately I need the time in my database, so the orderCreationDate needs to stay datetime/timestamp.
So how can I query for a specific date, ignoring the time?
thanks!
Your call to setParameter needs to pass in the temporal type argument also, defining what to use for comparison.
http://www.datanucleus.org/javadocs/javax.persistence/2.1/javax/persistence/Query.html#setParameter-java.lang.String-java.util.Date-javax.persistence.TemporalType-
I use MongoTemplate to handle MongoDB
I want update documents' column to current time
In Mongodb command-line client, it will work with
db.collectionName.update({_id:1}, {timeCol: new Timestamp()});
or
db.collectionName.update({_id:1}, {timeCol: new Date()});
But I don't know how I do that by using mongoTemplate.
Update update;
update.set("timeCol", "new Timestamp()"); // of course, it doesn't work
Plz help me
Update Collection like this from Spring-data-mongodb 1.6 version, this will use MongoDb current Date
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(1));
Update update = new Update();
update.currentDate("timeCol")
mongoOperations.updateFirst(query, update, "collectionName");
If you want Timestamp use update.currentTimestamp(key); instead of update.currentDate(key)
Build current timestamp as
Date currentDate = new Date();
Timestamp timeStamp = new Timestamp(currentDate.getTime());
Then update collection like this :
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(1));
Update update = new Update();
update.set("timeCol", timeStamp);
mongoOperations.updateFirst(query, update, "collectionName");
I have date in new Date() format. I need to convert to SQLite date time format.(2014-01-05T00:00:00.000Z). Is it possible to do so??
var date = new Date();
now I need to convert this date to SQLite datetime format like (2014-01-05T00:00:00.000Z).
Try:
var date = new Date();
var sqllite_date = date.toISOString();
Use this
// Create an instance of the Date class
var date = new Date();
// Convert it to an ISO string
var sqliteDate = Date.toISOString();
take a look at this W3schools
You can use strftime, from date and time functions.
SELECT strftime('%d-%m-%Y', 'now')
Output
10-06-2010
Hope this helps ..:)