Spring data MongoDB get document closes to the give date and time












0















I'm trying to implement the following working mongoDb query in Spring Data using MongoTemplate



db.events.aggregate([



    { 
"$match" : {
"someId" : "5bf5372d6518b0b6315d7a29"
}
},
{
"$project" :{
"totalValue" : 1,
"diff" : {
"$abs":{
"$subtract" : [ "$dateTime" , new Date(2018,11,21,9,0,0)]
}
}
}
},
{
"$sort":{
"diff":1
}
},
{
"$limit":1
}
])


I have got upto this point



MatchOperation match = Aggregation.match(Criteria.where("someId").is("idValue"));
ProjectionOperation projection = Aggregation.project().and("dateTime")
.minus(format.parse(LocalDate.now().toString() + " 09:00:00").getTime()).as("diff")
.and("totalValue").as("totalValue");
SortOperation sort = Aggregation.sort(Sort.Direction.ASC, "diff");
AggregationResults<Document> result = mongoTemplate
.aggregate(Aggregation.newAggregation(match, projection, sort), Input.class, Document.class);


Following is the documents



db.event.insert([
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,00,11), totalValue: .15},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,00,01), totalValue: .05},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,01,11), totalValue: .25},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,01,51), totalValue: .45},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,8,59,02), totalValue: .04},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,8,55,11), totalValue: .95},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,21,9,01,51), totalValue: .051},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,21,8,59,02), totalValue: .041},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,21,8,55,11), totalValue: .053},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,22,8,59,02), totalValue: .974},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,22,8,55,11), totalValue: .9},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,22,9,01,51), totalValue: .151},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,22,8,59,02), totalValue: .241}])


Expected output is : 0.05 (as it is close to the date time 21-11-2018 09:00:00)










share|improve this question























  • That's not really wise. That calculation requires a collection scan, which means reading every document in your collection. It would be far faster ( as well as easy to implement ) to simply get the first document which is "greater than" and the first which is "less than" and then compare those two documents. In this case the cost of two requests is far less than the cost of the proposed aggregation code. Use in production at your own peril, and if this were for my application, I would reject the code commit for something like this.

    – Neil Lunn
    Nov 22 '18 at 7:16













  • I'm just a newbie to mongoDB pls excuse my aggregation method.Thank you for valuable comment, I was successfully achieve it in the way you mentioned.

    – Kannan
    Nov 22 '18 at 10:33
















0















I'm trying to implement the following working mongoDb query in Spring Data using MongoTemplate



db.events.aggregate([



    { 
"$match" : {
"someId" : "5bf5372d6518b0b6315d7a29"
}
},
{
"$project" :{
"totalValue" : 1,
"diff" : {
"$abs":{
"$subtract" : [ "$dateTime" , new Date(2018,11,21,9,0,0)]
}
}
}
},
{
"$sort":{
"diff":1
}
},
{
"$limit":1
}
])


I have got upto this point



MatchOperation match = Aggregation.match(Criteria.where("someId").is("idValue"));
ProjectionOperation projection = Aggregation.project().and("dateTime")
.minus(format.parse(LocalDate.now().toString() + " 09:00:00").getTime()).as("diff")
.and("totalValue").as("totalValue");
SortOperation sort = Aggregation.sort(Sort.Direction.ASC, "diff");
AggregationResults<Document> result = mongoTemplate
.aggregate(Aggregation.newAggregation(match, projection, sort), Input.class, Document.class);


Following is the documents



db.event.insert([
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,00,11), totalValue: .15},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,00,01), totalValue: .05},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,01,11), totalValue: .25},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,01,51), totalValue: .45},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,8,59,02), totalValue: .04},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,8,55,11), totalValue: .95},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,21,9,01,51), totalValue: .051},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,21,8,59,02), totalValue: .041},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,21,8,55,11), totalValue: .053},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,22,8,59,02), totalValue: .974},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,22,8,55,11), totalValue: .9},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,22,9,01,51), totalValue: .151},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,22,8,59,02), totalValue: .241}])


Expected output is : 0.05 (as it is close to the date time 21-11-2018 09:00:00)










share|improve this question























  • That's not really wise. That calculation requires a collection scan, which means reading every document in your collection. It would be far faster ( as well as easy to implement ) to simply get the first document which is "greater than" and the first which is "less than" and then compare those two documents. In this case the cost of two requests is far less than the cost of the proposed aggregation code. Use in production at your own peril, and if this were for my application, I would reject the code commit for something like this.

    – Neil Lunn
    Nov 22 '18 at 7:16













  • I'm just a newbie to mongoDB pls excuse my aggregation method.Thank you for valuable comment, I was successfully achieve it in the way you mentioned.

    – Kannan
    Nov 22 '18 at 10:33














0












0








0








I'm trying to implement the following working mongoDb query in Spring Data using MongoTemplate



db.events.aggregate([



    { 
"$match" : {
"someId" : "5bf5372d6518b0b6315d7a29"
}
},
{
"$project" :{
"totalValue" : 1,
"diff" : {
"$abs":{
"$subtract" : [ "$dateTime" , new Date(2018,11,21,9,0,0)]
}
}
}
},
{
"$sort":{
"diff":1
}
},
{
"$limit":1
}
])


I have got upto this point



MatchOperation match = Aggregation.match(Criteria.where("someId").is("idValue"));
ProjectionOperation projection = Aggregation.project().and("dateTime")
.minus(format.parse(LocalDate.now().toString() + " 09:00:00").getTime()).as("diff")
.and("totalValue").as("totalValue");
SortOperation sort = Aggregation.sort(Sort.Direction.ASC, "diff");
AggregationResults<Document> result = mongoTemplate
.aggregate(Aggregation.newAggregation(match, projection, sort), Input.class, Document.class);


Following is the documents



db.event.insert([
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,00,11), totalValue: .15},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,00,01), totalValue: .05},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,01,11), totalValue: .25},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,01,51), totalValue: .45},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,8,59,02), totalValue: .04},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,8,55,11), totalValue: .95},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,21,9,01,51), totalValue: .051},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,21,8,59,02), totalValue: .041},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,21,8,55,11), totalValue: .053},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,22,8,59,02), totalValue: .974},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,22,8,55,11), totalValue: .9},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,22,9,01,51), totalValue: .151},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,22,8,59,02), totalValue: .241}])


Expected output is : 0.05 (as it is close to the date time 21-11-2018 09:00:00)










share|improve this question














I'm trying to implement the following working mongoDb query in Spring Data using MongoTemplate



db.events.aggregate([



    { 
"$match" : {
"someId" : "5bf5372d6518b0b6315d7a29"
}
},
{
"$project" :{
"totalValue" : 1,
"diff" : {
"$abs":{
"$subtract" : [ "$dateTime" , new Date(2018,11,21,9,0,0)]
}
}
}
},
{
"$sort":{
"diff":1
}
},
{
"$limit":1
}
])


I have got upto this point



MatchOperation match = Aggregation.match(Criteria.where("someId").is("idValue"));
ProjectionOperation projection = Aggregation.project().and("dateTime")
.minus(format.parse(LocalDate.now().toString() + " 09:00:00").getTime()).as("diff")
.and("totalValue").as("totalValue");
SortOperation sort = Aggregation.sort(Sort.Direction.ASC, "diff");
AggregationResults<Document> result = mongoTemplate
.aggregate(Aggregation.newAggregation(match, projection, sort), Input.class, Document.class);


Following is the documents



db.event.insert([
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,00,11), totalValue: .15},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,00,01), totalValue: .05},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,01,11), totalValue: .25},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,9,01,51), totalValue: .45},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,8,59,02), totalValue: .04},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,21,8,55,11), totalValue: .95},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,21,9,01,51), totalValue: .051},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,21,8,59,02), totalValue: .041},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,21,8,55,11), totalValue: .053},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,22,8,59,02), totalValue: .974},
{someId: "5bf5372d6518b0b6315d7a29", dateTime: new Date(2018,11,22,8,55,11), totalValue: .9},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,22,9,01,51), totalValue: .151},
{someId: "5bf5372d6518b0b6315d7a28", dateTime: new Date(2018,11,22,8,59,02), totalValue: .241}])


Expected output is : 0.05 (as it is close to the date time 21-11-2018 09:00:00)







mongodb spring-data spring-data-mongodb mongotemplate






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 7:07









KannanKannan

11




11













  • That's not really wise. That calculation requires a collection scan, which means reading every document in your collection. It would be far faster ( as well as easy to implement ) to simply get the first document which is "greater than" and the first which is "less than" and then compare those two documents. In this case the cost of two requests is far less than the cost of the proposed aggregation code. Use in production at your own peril, and if this were for my application, I would reject the code commit for something like this.

    – Neil Lunn
    Nov 22 '18 at 7:16













  • I'm just a newbie to mongoDB pls excuse my aggregation method.Thank you for valuable comment, I was successfully achieve it in the way you mentioned.

    – Kannan
    Nov 22 '18 at 10:33



















  • That's not really wise. That calculation requires a collection scan, which means reading every document in your collection. It would be far faster ( as well as easy to implement ) to simply get the first document which is "greater than" and the first which is "less than" and then compare those two documents. In this case the cost of two requests is far less than the cost of the proposed aggregation code. Use in production at your own peril, and if this were for my application, I would reject the code commit for something like this.

    – Neil Lunn
    Nov 22 '18 at 7:16













  • I'm just a newbie to mongoDB pls excuse my aggregation method.Thank you for valuable comment, I was successfully achieve it in the way you mentioned.

    – Kannan
    Nov 22 '18 at 10:33

















That's not really wise. That calculation requires a collection scan, which means reading every document in your collection. It would be far faster ( as well as easy to implement ) to simply get the first document which is "greater than" and the first which is "less than" and then compare those two documents. In this case the cost of two requests is far less than the cost of the proposed aggregation code. Use in production at your own peril, and if this were for my application, I would reject the code commit for something like this.

– Neil Lunn
Nov 22 '18 at 7:16







That's not really wise. That calculation requires a collection scan, which means reading every document in your collection. It would be far faster ( as well as easy to implement ) to simply get the first document which is "greater than" and the first which is "less than" and then compare those two documents. In this case the cost of two requests is far less than the cost of the proposed aggregation code. Use in production at your own peril, and if this were for my application, I would reject the code commit for something like this.

– Neil Lunn
Nov 22 '18 at 7:16















I'm just a newbie to mongoDB pls excuse my aggregation method.Thank you for valuable comment, I was successfully achieve it in the way you mentioned.

– Kannan
Nov 22 '18 at 10:33





I'm just a newbie to mongoDB pls excuse my aggregation method.Thank you for valuable comment, I was successfully achieve it in the way you mentioned.

– Kannan
Nov 22 '18 at 10:33












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425543%2fspring-data-mongodb-get-document-closes-to-the-give-date-and-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425543%2fspring-data-mongodb-get-document-closes-to-the-give-date-and-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory