How to fetch matching element in array using timestamp or date in reactive Spring Mongodb












0















First I'm trying to fetch all documents in a collection, some of the documents has an array of objects "events" which has the "latestTimeStamp" property field.



Example data:



[
{
"_id": "5c1f7054b8fcd6574162a594",
"userId": "35",
"createdDate": "Sun Dec 23 15:24:04 GST 2018",
"events": [
{
"latestTimeStamp": "2018-12-31T06:08:49.808Z"

},
{
"latestTimeStamp": "2018-12-31T06:09:13.235Z"

},
{
"latestTimeStamp": "2018-12-31T06:21:36.666Z"

},
{
"latestTimeStamp": "2018-12-31T06:21:59.427Z"

},
{
"latestTimeStamp": "2018-12-31T06:22:19.858Z"

},
{
"latestTimeStamp": "2018-12-31T06:22:43.728Z"

}
]
},
{
"_id": "5c2875f9fc441802398b8925",
"userId": "35",
"createdDate": "Sun Dec 30 11:38:33 GST 2018",

"events": [
{
"latestTimeStamp": null

},
{
"latestTimeStamp": "Dec 30, 2018, 12:02:07 PM"

},
{
"latestTimeStamp": "Dec 30, 2018, 12:03:08 PM"

},
{
"latestTimeStamp": "Dec 31, 2018, 10:34:12 AM"

},
{
"latestTimeStamp": "Dec 31, 2018, 10:35:21 AM"

},
{
"latestTimeStamp": "Dec 31, 2018, 10:36:21 AM"

}
]
},
{
"_id": "5c28b985fc4418461a38ee79",
"userId": "5c1894a44d27fc000634e200",
"createdDate": "Sun Dec 30 16:26:45 GST 2018",

"events": [
{
"latestTimeStamp": "2018-12-30T12:26:33.666251Z"

},
{
"latestTimeStamp": "2018-12-30T12:28:56.035Z"

},

{
"latestTimeStamp": "2018-12-30T13:00:01.212Z"
},
{
"latestTimeStamp": "2018-12-30T13:01:16.183Z"

},
{
"latestTimeStamp": "2018-12-30T13:28:47.290Z"

},
{
"latestTimeStamp": "2018-12-30T13:30:03.035Z"

},
{
"latestTimeStamp": "2018-12-30T13:26:16.981Z"

},
{
"latestTimeStamp": "2018-12-30T13:27:31.072Z"


},
{
"latestTimeStamp": "2018-12-30T13:32:31.278Z"

},
{
"latestTimeStamp": "2018-12-30T13:31:15.812Z"

},
{
"latestTimeStamp": "2018-12-30T13:33:46.519Z"

},
{
"latestTimeStamp": "2018-12-30T13:35:02.424Z"

},
{
"latestTimeStamp": "2018-12-30T13:36:15.887Z"

},
{
"latestTimeStamp": "2018-12-30T13:37:31.641Z"

},
{
"latestTimeStamp": "2018-12-30T13:38:46.618Z"

},
{
"latestTimeStamp": "2018-12-30T13:40:01.975Z"

},
{
"latestTimeStamp": "2018-12-30T13:41:48.499Z"

},
{
"latestTimeStamp": "2018-12-30T13:44:08.981Z"

},
{
"latestTimeStamp": "2018-12-30T13:44:17.154Z"
},
{
"latestTimeStamp": "2018-12-30T13:44:23.837Z"

},
{
"latestTimeStamp": "2018-12-30T13:44:20.137Z
}
]
}]


I want to fetch all documents in collections and only one object in the array objects of "events" by "latestTimeStamp"



I was able to fetch all documents with only one object of "events" by using $slice method:



  Query query = new Query();
query.fields().include("events").slice("events", -1,1);
return reactiveMongoTemplate.find(query, Mission.class);


My question is , is there a better way to achieve fetching it? like max date or latest time stamp? or using aggregate ?










share|improve this question





























    0















    First I'm trying to fetch all documents in a collection, some of the documents has an array of objects "events" which has the "latestTimeStamp" property field.



    Example data:



    [
    {
    "_id": "5c1f7054b8fcd6574162a594",
    "userId": "35",
    "createdDate": "Sun Dec 23 15:24:04 GST 2018",
    "events": [
    {
    "latestTimeStamp": "2018-12-31T06:08:49.808Z"

    },
    {
    "latestTimeStamp": "2018-12-31T06:09:13.235Z"

    },
    {
    "latestTimeStamp": "2018-12-31T06:21:36.666Z"

    },
    {
    "latestTimeStamp": "2018-12-31T06:21:59.427Z"

    },
    {
    "latestTimeStamp": "2018-12-31T06:22:19.858Z"

    },
    {
    "latestTimeStamp": "2018-12-31T06:22:43.728Z"

    }
    ]
    },
    {
    "_id": "5c2875f9fc441802398b8925",
    "userId": "35",
    "createdDate": "Sun Dec 30 11:38:33 GST 2018",

    "events": [
    {
    "latestTimeStamp": null

    },
    {
    "latestTimeStamp": "Dec 30, 2018, 12:02:07 PM"

    },
    {
    "latestTimeStamp": "Dec 30, 2018, 12:03:08 PM"

    },
    {
    "latestTimeStamp": "Dec 31, 2018, 10:34:12 AM"

    },
    {
    "latestTimeStamp": "Dec 31, 2018, 10:35:21 AM"

    },
    {
    "latestTimeStamp": "Dec 31, 2018, 10:36:21 AM"

    }
    ]
    },
    {
    "_id": "5c28b985fc4418461a38ee79",
    "userId": "5c1894a44d27fc000634e200",
    "createdDate": "Sun Dec 30 16:26:45 GST 2018",

    "events": [
    {
    "latestTimeStamp": "2018-12-30T12:26:33.666251Z"

    },
    {
    "latestTimeStamp": "2018-12-30T12:28:56.035Z"

    },

    {
    "latestTimeStamp": "2018-12-30T13:00:01.212Z"
    },
    {
    "latestTimeStamp": "2018-12-30T13:01:16.183Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:28:47.290Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:30:03.035Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:26:16.981Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:27:31.072Z"


    },
    {
    "latestTimeStamp": "2018-12-30T13:32:31.278Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:31:15.812Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:33:46.519Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:35:02.424Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:36:15.887Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:37:31.641Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:38:46.618Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:40:01.975Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:41:48.499Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:44:08.981Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:44:17.154Z"
    },
    {
    "latestTimeStamp": "2018-12-30T13:44:23.837Z"

    },
    {
    "latestTimeStamp": "2018-12-30T13:44:20.137Z
    }
    ]
    }]


    I want to fetch all documents in collections and only one object in the array objects of "events" by "latestTimeStamp"



    I was able to fetch all documents with only one object of "events" by using $slice method:



      Query query = new Query();
    query.fields().include("events").slice("events", -1,1);
    return reactiveMongoTemplate.find(query, Mission.class);


    My question is , is there a better way to achieve fetching it? like max date or latest time stamp? or using aggregate ?










    share|improve this question



























      0












      0








      0








      First I'm trying to fetch all documents in a collection, some of the documents has an array of objects "events" which has the "latestTimeStamp" property field.



      Example data:



      [
      {
      "_id": "5c1f7054b8fcd6574162a594",
      "userId": "35",
      "createdDate": "Sun Dec 23 15:24:04 GST 2018",
      "events": [
      {
      "latestTimeStamp": "2018-12-31T06:08:49.808Z"

      },
      {
      "latestTimeStamp": "2018-12-31T06:09:13.235Z"

      },
      {
      "latestTimeStamp": "2018-12-31T06:21:36.666Z"

      },
      {
      "latestTimeStamp": "2018-12-31T06:21:59.427Z"

      },
      {
      "latestTimeStamp": "2018-12-31T06:22:19.858Z"

      },
      {
      "latestTimeStamp": "2018-12-31T06:22:43.728Z"

      }
      ]
      },
      {
      "_id": "5c2875f9fc441802398b8925",
      "userId": "35",
      "createdDate": "Sun Dec 30 11:38:33 GST 2018",

      "events": [
      {
      "latestTimeStamp": null

      },
      {
      "latestTimeStamp": "Dec 30, 2018, 12:02:07 PM"

      },
      {
      "latestTimeStamp": "Dec 30, 2018, 12:03:08 PM"

      },
      {
      "latestTimeStamp": "Dec 31, 2018, 10:34:12 AM"

      },
      {
      "latestTimeStamp": "Dec 31, 2018, 10:35:21 AM"

      },
      {
      "latestTimeStamp": "Dec 31, 2018, 10:36:21 AM"

      }
      ]
      },
      {
      "_id": "5c28b985fc4418461a38ee79",
      "userId": "5c1894a44d27fc000634e200",
      "createdDate": "Sun Dec 30 16:26:45 GST 2018",

      "events": [
      {
      "latestTimeStamp": "2018-12-30T12:26:33.666251Z"

      },
      {
      "latestTimeStamp": "2018-12-30T12:28:56.035Z"

      },

      {
      "latestTimeStamp": "2018-12-30T13:00:01.212Z"
      },
      {
      "latestTimeStamp": "2018-12-30T13:01:16.183Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:28:47.290Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:30:03.035Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:26:16.981Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:27:31.072Z"


      },
      {
      "latestTimeStamp": "2018-12-30T13:32:31.278Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:31:15.812Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:33:46.519Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:35:02.424Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:36:15.887Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:37:31.641Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:38:46.618Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:40:01.975Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:41:48.499Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:44:08.981Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:44:17.154Z"
      },
      {
      "latestTimeStamp": "2018-12-30T13:44:23.837Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:44:20.137Z
      }
      ]
      }]


      I want to fetch all documents in collections and only one object in the array objects of "events" by "latestTimeStamp"



      I was able to fetch all documents with only one object of "events" by using $slice method:



        Query query = new Query();
      query.fields().include("events").slice("events", -1,1);
      return reactiveMongoTemplate.find(query, Mission.class);


      My question is , is there a better way to achieve fetching it? like max date or latest time stamp? or using aggregate ?










      share|improve this question
















      First I'm trying to fetch all documents in a collection, some of the documents has an array of objects "events" which has the "latestTimeStamp" property field.



      Example data:



      [
      {
      "_id": "5c1f7054b8fcd6574162a594",
      "userId": "35",
      "createdDate": "Sun Dec 23 15:24:04 GST 2018",
      "events": [
      {
      "latestTimeStamp": "2018-12-31T06:08:49.808Z"

      },
      {
      "latestTimeStamp": "2018-12-31T06:09:13.235Z"

      },
      {
      "latestTimeStamp": "2018-12-31T06:21:36.666Z"

      },
      {
      "latestTimeStamp": "2018-12-31T06:21:59.427Z"

      },
      {
      "latestTimeStamp": "2018-12-31T06:22:19.858Z"

      },
      {
      "latestTimeStamp": "2018-12-31T06:22:43.728Z"

      }
      ]
      },
      {
      "_id": "5c2875f9fc441802398b8925",
      "userId": "35",
      "createdDate": "Sun Dec 30 11:38:33 GST 2018",

      "events": [
      {
      "latestTimeStamp": null

      },
      {
      "latestTimeStamp": "Dec 30, 2018, 12:02:07 PM"

      },
      {
      "latestTimeStamp": "Dec 30, 2018, 12:03:08 PM"

      },
      {
      "latestTimeStamp": "Dec 31, 2018, 10:34:12 AM"

      },
      {
      "latestTimeStamp": "Dec 31, 2018, 10:35:21 AM"

      },
      {
      "latestTimeStamp": "Dec 31, 2018, 10:36:21 AM"

      }
      ]
      },
      {
      "_id": "5c28b985fc4418461a38ee79",
      "userId": "5c1894a44d27fc000634e200",
      "createdDate": "Sun Dec 30 16:26:45 GST 2018",

      "events": [
      {
      "latestTimeStamp": "2018-12-30T12:26:33.666251Z"

      },
      {
      "latestTimeStamp": "2018-12-30T12:28:56.035Z"

      },

      {
      "latestTimeStamp": "2018-12-30T13:00:01.212Z"
      },
      {
      "latestTimeStamp": "2018-12-30T13:01:16.183Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:28:47.290Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:30:03.035Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:26:16.981Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:27:31.072Z"


      },
      {
      "latestTimeStamp": "2018-12-30T13:32:31.278Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:31:15.812Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:33:46.519Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:35:02.424Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:36:15.887Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:37:31.641Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:38:46.618Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:40:01.975Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:41:48.499Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:44:08.981Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:44:17.154Z"
      },
      {
      "latestTimeStamp": "2018-12-30T13:44:23.837Z"

      },
      {
      "latestTimeStamp": "2018-12-30T13:44:20.137Z
      }
      ]
      }]


      I want to fetch all documents in collections and only one object in the array objects of "events" by "latestTimeStamp"



      I was able to fetch all documents with only one object of "events" by using $slice method:



        Query query = new Query();
      query.fields().include("events").slice("events", -1,1);
      return reactiveMongoTemplate.find(query, Mission.class);


      My question is , is there a better way to achieve fetching it? like max date or latest time stamp? or using aggregate ?







      spring-webflux spring-mongodb spring-mongo mongotemplate






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 7:30







      Kamal

















      asked Jan 1 at 10:12









      KamalKamal

      5091616




      5091616
























          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%2f53994626%2fhow-to-fetch-matching-element-in-array-using-timestamp-or-date-in-reactive-sprin%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%2f53994626%2fhow-to-fetch-matching-element-in-array-using-timestamp-or-date-in-reactive-sprin%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

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]