Problem with matching date-time fields using MongoDB Spring Data Aggregation - mongodb

Just want to do a MatchOperation on date in long format (date in seconds) in MongoDB
The fromDate and toDate Variables are with data type as long.
It's divided with 1000 to get the data in seconds format and not in milliseconds.
long fromDate = new Date().getTime()/1000;
The datetime field format is in mongo Document
"datetime" : NumberInt(1595745447),
MatchOperation matchDate = Aggregation.match(Criteria.where("datetime")
.gte(fromDate)
.lte(toDate));
the aggregation formed using this is
db.some_collection.aggregate([{ "$match" : { "datetime" : { "$gte" : { "$numberLong" : "1595356200"}, "$lte" : { "$numberLong" : "1595375999"}}}}])
But its not working. It's giving 0 results.
But when the same is just modified (as shown bellow) a bit manually by jus removing
"$numberLong"
it's giving results and Filters as expected.
db.some_collection.aggregate([{ "$match" : { "datetime" : { "$gte" :1595356200, "$lte" : 1595375999}}}])
Can any one help me out with this?
Thanks in advance.

There is a syntax error. NumberLong is a function not an operator.
Refer
Change
{ "$numberLong" : "1595356200"},
To
NumberLong("1595356200")

Related

convert timestamp to ISO date in Mongodb version 2.4

I have mongodb version 2.4.10 installed. The structure of the document is :-
{
"_id" : ObjectId("5d15f245f4dda1e055091ae1"),
"name" : "test site service",
"starFromTimestamp" : NumberLong(1559275200),
"toTimestamp" : NumberLong(1561867200),
"uuid" : "ssg-5d15f245f2893825813309"
}
I excuted the following code in Mongo shell in order to convert timestamp to ISODate
db.servicesitegroup.find().forEach(function(doc) {
doc.startISODate=new Date(doc.starFromTimestamp);
db.servicesitegroup.save(doc);
})
The document got updated and the resultset looks like:-
{
"_id" : ObjectId("5d15f245f4dda1e055091ae1"),
"name" : "test site service",
"starFromTimestamp" : NumberLong(1559275200),
"toTimestamp" : NumberLong(1561867200),
"uuid" : "ssg-5d15f245f2893825813309",
"startISODate" : ISODate("1970-01-19T01:07:55.200Z")
}
If I use the timestamp converter, then the value of 1559275200 amounts to Friday, May 31, 2019 4:00:00 AM . Why is the timestamp not being converted to the correct value? Can anyone guide me here.
I needed to multiply the timestamp value with 1000 .
db.servicesitegroup.find().forEach(function(doc) {
doc.startISODate=new Date(doc.starFromTimestamp * 1000);
db.servicesitegroup.save(doc);
})
The code above gives me the correct output.

I do not get an answer when looking for higher / lower records than the current date

I'm using robomongo to perform a search. I want to obtain the records that the "FECHA_INICIO" field is greater than or equal to today's date.
{
"_id" : ObjectId("5bda1145b82d4c21a8e56890"),
"COD_TRAFO" : "N42001",
"NIU" : "334099968",
"FECHA_INICIO" : "2018-012-29",
"FECHA_FIN" : "2018-11-10",
"HORA_INICIO" : "9:00",
"HORA_FIN" : "16:00",
"ESTADO" : "ABIERTO",
"ORDEN_OP" : "693583"
}
but with this query:
db.getCollection('mycollection').find( {"FECHA_INICIO":{ $gte: new Date("YYYY-mm-dd") }} )
I do not get results, what am I doing wrong? Thank you

mongodb time duration between between two dates

I want to find the time duration in the form of days:hours:minutes by passing two dates in mongodb for all document.
Example document present in mongodb:
{
"_id" : ObjectId("5b3b303f4a05d1673d9bfa31"),
"customerID" : "1",
"latitude" : "13.035770",
"longitude" : "77.597022",
"loc_update_time" : 1533021761818.0,
"loc" : [
77.597022,
13.03577
],
"datetime" : ISODate("2018-08-01T09:43:25.729Z")
}
one date is present in douctment and another date i will pass in query.
please help me
Thank you

MONGODB - cast type of every object in array of objects

I have MongoDB Collection where some documents have arrays of objects. One of the fields of this objects is timestamp.
The problem is that historically some of timestamp values are Strings (e.g. '2018-02-25T13:33:56.675000') or Date and some of them are Double (e.g. 1528108521726.26).
I have to convert all of them to Double.
I've built the query to get all the documents with the problematic type:
db.getCollection('Cases').find({sent_messages: {$elemMatch:{timestamp: {$type:[2, 9]}}}})
And I also know how to convert Date-string to double using JS:
new Date("2018-02-18T06:39:20.797Z").getTime()
> 1518935960797
But I can't build the proper query to perform the update.
Here is an example of such a document:
{
"_id" : ObjectId("6c88f656532aab00050dc023"),
"created_at" : ISODate("2018-05-18T03:43:18.986Z"),
"updated_at" : ISODate("2018-05-18T06:39:20.798Z"),
"sent_messages" : [
{
"timestamp" : ISODate("2018-02-18T06:39:20.797Z"),
"text" : "Hey",
"sender" : "me"
}
],
"status" : 1
}
After the update it should be:
{
"_id" : ObjectId("6c88f656532aab00050dc023"),
"created_at" : ISODate("2018-05-18T03:43:18.986Z"),
"updated_at" : ISODate("2018-05-18T06:39:20.798Z"),
"sent_messages" : [
{
"timestamp" : 1518935960797.00,
"text" : "Hey",
"sender" : "me"
}
],
"status" : 1
}
As per your question, you are trying to fetch the record first.
db.getCollection('Cases').find({sent_messages: {$elemMatch:{timestamp: {$type:[2, 9]}}}})
Then convert date in JS:
new Date("2018-02-18T06:39:20.797Z").getTime()
And then this is an update query:
db.getCollection('Cases').updateOne({_id:ObjectId("6c88f656532aab00050dc023")}, { $set: { "sent_messages.$.timestamp" : "218392712937.0" }})
And if you want to update all records then you should write some forEach mechanism. I think you have already this implemented.
Hope this may help you.
Finally I just do it with JS code that can be run in mongo console:
db.getCollection('Cases').find({sent_messages: {$elemMatch:{timestamp: {$type:[2, 9]}}}}).forEach(function(doc) {
print('=================');
print(JSON.stringify(doc));
doc.sent_messages.forEach(function(msg){
var dbl = new Date(msg.timestamp).getTime();
print(dbl);
msg.timestamp = dbl;
});
print(JSON.stringify(doc))
db.Cases.save(doc);
} )
Thanks all for your help!

Query date based using milliseconds time on MongoDB

I need to search all records that match a date condition. On MongoDB I've a bunch of data like this:
{
"_id" : "9ed3b937-0f43-4613-bd58-cb739a8c5bf6",
"userModels" : {
"5080" : {
"generated_date_timestamp" : NumberLong(1413382499442),
"model_id" : 5080,
},
}
"values" : {}
}
I'm not able to convert that NumberLong in something that can be used by this query:
db.anonProfile.find({
"userModels.5080.generated_date_timestamp" : { "$gte" : ISODate("2013-10-01T00:00:00.000Z") }
});
"_id" has been saved as String so I cannot use for a ObjectID search. [btw, is it possible to do?]
Any clue?
Tnx, Andrea
You can query by
db.anonProfile.find({
"userModels.5080.generated_date_timestamp" : { "$gte" : ISODate("2013-10-01T00:00:00.000Z").getTime() }
});