I have loaded data into mongoDB in below format:
{
"horizontalAccuracy" : 3.0,
"timestamp" : 1551390869,
"datetime" : ISODate("2019-02-28T18:30:00.000+0000"),
"month" : 3,
"year" : 2019,
"country" : "MYS"
}
Now I want to run a query on this records, select records which are created between 02:00PM to 04:00PM. I would be glad to have any suggestions.
Related
I have to make an API whose endpoint will be like day=1&time=1000 and by using these query parameters I have to write a mongo query, JSON will be like
"_id" : ObjectId("62257ddd76b35400010e7015"),
"applyOffersOn" : [
{
"day" : 1,
"startTime" : NumberLong("1646625639561"),
"endTime" : NumberLong("1646631039561"),
"startTimeFormat" : NumberLong(930),
"endTimeFormat" : NumberLong(1100)
},
{
"day" : 2,
"startTime" : NumberLong("1646625639561"),
"endTime" : NumberLong("1646631039561"),
"startTimeFormat" : NumberLong(930),
"endTimeFormat" : NumberLong(1100)
},
{
"day" : 3,
"startTime" : NumberLong("1646625639561"),
"endTime" : NumberLong("1646631039561"),
"startTimeFormat" : NumberLong(1930),
"endTimeFormat" : NumberLong(2100)
}
],
}```
the query should be like I have to first check for a particular object using objectId then finding the object, check for the day is present as 1 and then check for the time that it will be between
startTimeFormat and endTimeFormat if all these conditions satisfy then return the whole object
I have written the query but it either works for the day or for a time but it should work like it check for both condition
I think this will help ,
##elemMatch - it will loop you through all elements of array.
const answer = <your model schema>.findById(req.params.id).find({applyOffersOn:{$elemMatch:{day:<your day from query> } }}).find({applyOffersOn:{$elemMatch : {startTimeFormat: {$lte:<your time from query> },endTimeFormat:{$gte:<your time from query>} }}})
So this is my data in mongodb
{
"_id" : ObjectId("60f67dc955784b233692a0f2"),
"Id" : "9153",
"InfoList" : [
{
"itemId" : "42342",
"price" : 1009.0,
"date" : ISODate("2021-01-01T08:30:36.131Z")
}
]
}
{
"_id" : ObjectId("6105668a55784bd00ef3ebc6"),
"Id" : "894249",
"InfoList" : [
{
"itemId" : "42342",
"price" : 23.0,
"date" : ISODate("2021-01-01T08:30:36.131Z")
},
{
"itemId" : "3221",
"price" : 44554.0,
"date" : ISODate("2013-07-31T15:05:10.042Z")
}
]
}
I want to find all the items in InfoList for all the documents whose date is less than 2021-02-09 and then delete them.
This is the code that I am using
Query query = new Query();
query.addCriteria(Criteria.where("InfoList")
.elemMatch(Criteria
.where("date")
.lte(date)));
return mongoTemplate.findAllAndRemove(query,ProductInfo.class, CollectionName);
But this code is neither finding the documents which have date < 2021-02-01 nor deleting them. Any suggestions regarding what might be wrong here ?
I'm not familiar with Mongo query I know mongoose more
But do it like this 1-get all the of the object
It will return an array
On this array
2-do an if to check if the date is less than 2021-02-09 if yes remove it using its id
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
We have been using the following command in MongoDB 2.4.* to create a field with auto-generated time-stamp values in inserted documents while not touching the same field for updates:
db.entity.update({EntityId : "ID01"},
{$setOnInsert : {Created : Timestamp(0,0)}, $set : {Name: "Abc"}},
{upsert : true})
This did work fine in 2.4.* with the inserted document having the field populated with the current time-stamp value:
{
"_id" : ObjectId("54e312dc649bb37cf4ab413a"),
"Created" : Timestamp(1424167644, 1),
"Name" : "Abc",
"EntityId" : "ID01"
}
However, when we run the same command in MongoDB 2.6.7, we end up with an empty time stamp:
{
"_id" : ObjectId("54e312e712e476107488a632"),
"EntityId" : "ID01",
"Created" : Timestamp(0, 0),
"Name" : "Abc"
}
Is there a way to reproduce 2.4 behavior in 2.6?
I have a document and I need to query mongodb database to return me all the documents which was inserted after current document.
Is it possible and how to do that query?
If you do not override the default _id field you can use that objectID (see the mongodb docs) to make a comparison by time. For instance, the following query will find all the documents that are inserted after curDoc has been inserted (assuming none overwrite the _id field):
>db.test.find({ _id : {$gt : curDoc._id}})
Note that these timestamps are not super granular, if you would like a finer grained view of the time that documents are inserted I encourage you to add your own timestamp field to the documents you are inserting and use that field to make such queries.
If you are using Insert time stamp as on of the parameter, you can query like below
> db.foo.find()
{ "_id" : ObjectId("514bf8bbbe11e483111af213"), "Name" : "abc", "Insert_time" : ISODate("2013-03-22T06:22:51.422Z") }
{ "_id" : ObjectId("514bf8c5be11e483111af214"), "Name" : "xyz", "Insert_time" : ISODate("2013-03-22T06:23:01.310Z") }
{ "_id" : ObjectId("514bf8cebe11e483111af215"), "Name" : "pqr", "Insert_time" : ISODate("2013-03-22T06:23:10.006Z") }
{ "_id" : ObjectId("514bf8eabe11e483111af216"), "Name" : "ijk", "Insert_time" : ISODate("2013-03-22T06:23:38.410Z") }
>
Here my Insert_time corresponds to the document inserted time, and following query will give you the documents after a particular Insert_time,
> db.foo.find({Insert_time:{$gt:ISODate("2013-03-22T06:22:51.422Z")}})
{ "_id" : ObjectId("514bf8c5be11e483111af214"), "Name" : "xyz", "Insert_time" : ISODate("2013-03-22T06:23:01.310Z") }
{ "_id" : ObjectId("514bf8cebe11e483111af215"), "Name" : "pqr", "Insert_time" : ISODate("2013-03-22T06:23:10.006Z") }
{ "_id" : ObjectId("514bf8eabe11e483111af216"), "Name" : "ijk", "Insert_time" : ISODate("2013-03-22T06:23:38.410Z") }
>