Couch DB query to fetch the records greater than a particular date












0














I am trying to fetch the records that has the "effectiveDateOfAction" field greater than Oct'2017. Please find the below 3 records.



{
"_id": "TRAN001",
"_rev": "13-59a53069c1ebd6ecfc23ca1dea0ba28f",
"effectiveDateOfAction": "10-30-2018",
"employeeName": "Kumar,Vinoth",
"transferReportID": "TRAN001",
"~version": "76:0"
}

{
"_id": "TRAN001",
"_rev": "12-c320c61168f5d6d020f971124cb395f2",
"effectiveDateOfAction": "05-10-2018",
"employeeName": "Vinoth",
"transferReportID": "TRAN002",
"~version": "77:0"
}

{
"_id": "TRAN003",
"_rev": "16-567a15e9ea7e2349d4c24816e7eafda3",
"effectiveDateOfAction": "10-20-2017",
"employeeName": "Kumar",
"transferReportID": "TRAN003",
"~version": "78:0"
}


Please find my query below which i tried.I am checking using Project Fauxton.



{"selector": {"$and": [{"transferReportID": {"$ne": null}},{"effectiveDateOfAction": {"$gt": "10-31-2017"}}]}}


Please help me getting the correct query.










share|improve this question
























  • "$gt": "10-31-2017" is a lexicographic string comparison, so 10-31-2018 will be greater than 01-01-2019. You may have to create a view, which emits your data sorted by date (see docs).
    – Enno
    Nov 20 at 9:40










  • @Enno Thank you for the reponse. I am new to couch DB query, If Possible can you please share the working example for this.
    – Priyanka
    Nov 20 at 11:33
















0














I am trying to fetch the records that has the "effectiveDateOfAction" field greater than Oct'2017. Please find the below 3 records.



{
"_id": "TRAN001",
"_rev": "13-59a53069c1ebd6ecfc23ca1dea0ba28f",
"effectiveDateOfAction": "10-30-2018",
"employeeName": "Kumar,Vinoth",
"transferReportID": "TRAN001",
"~version": "76:0"
}

{
"_id": "TRAN001",
"_rev": "12-c320c61168f5d6d020f971124cb395f2",
"effectiveDateOfAction": "05-10-2018",
"employeeName": "Vinoth",
"transferReportID": "TRAN002",
"~version": "77:0"
}

{
"_id": "TRAN003",
"_rev": "16-567a15e9ea7e2349d4c24816e7eafda3",
"effectiveDateOfAction": "10-20-2017",
"employeeName": "Kumar",
"transferReportID": "TRAN003",
"~version": "78:0"
}


Please find my query below which i tried.I am checking using Project Fauxton.



{"selector": {"$and": [{"transferReportID": {"$ne": null}},{"effectiveDateOfAction": {"$gt": "10-31-2017"}}]}}


Please help me getting the correct query.










share|improve this question
























  • "$gt": "10-31-2017" is a lexicographic string comparison, so 10-31-2018 will be greater than 01-01-2019. You may have to create a view, which emits your data sorted by date (see docs).
    – Enno
    Nov 20 at 9:40










  • @Enno Thank you for the reponse. I am new to couch DB query, If Possible can you please share the working example for this.
    – Priyanka
    Nov 20 at 11:33














0












0








0







I am trying to fetch the records that has the "effectiveDateOfAction" field greater than Oct'2017. Please find the below 3 records.



{
"_id": "TRAN001",
"_rev": "13-59a53069c1ebd6ecfc23ca1dea0ba28f",
"effectiveDateOfAction": "10-30-2018",
"employeeName": "Kumar,Vinoth",
"transferReportID": "TRAN001",
"~version": "76:0"
}

{
"_id": "TRAN001",
"_rev": "12-c320c61168f5d6d020f971124cb395f2",
"effectiveDateOfAction": "05-10-2018",
"employeeName": "Vinoth",
"transferReportID": "TRAN002",
"~version": "77:0"
}

{
"_id": "TRAN003",
"_rev": "16-567a15e9ea7e2349d4c24816e7eafda3",
"effectiveDateOfAction": "10-20-2017",
"employeeName": "Kumar",
"transferReportID": "TRAN003",
"~version": "78:0"
}


Please find my query below which i tried.I am checking using Project Fauxton.



{"selector": {"$and": [{"transferReportID": {"$ne": null}},{"effectiveDateOfAction": {"$gt": "10-31-2017"}}]}}


Please help me getting the correct query.










share|improve this question















I am trying to fetch the records that has the "effectiveDateOfAction" field greater than Oct'2017. Please find the below 3 records.



{
"_id": "TRAN001",
"_rev": "13-59a53069c1ebd6ecfc23ca1dea0ba28f",
"effectiveDateOfAction": "10-30-2018",
"employeeName": "Kumar,Vinoth",
"transferReportID": "TRAN001",
"~version": "76:0"
}

{
"_id": "TRAN001",
"_rev": "12-c320c61168f5d6d020f971124cb395f2",
"effectiveDateOfAction": "05-10-2018",
"employeeName": "Vinoth",
"transferReportID": "TRAN002",
"~version": "77:0"
}

{
"_id": "TRAN003",
"_rev": "16-567a15e9ea7e2349d4c24816e7eafda3",
"effectiveDateOfAction": "10-20-2017",
"employeeName": "Kumar",
"transferReportID": "TRAN003",
"~version": "78:0"
}


Please find my query below which i tried.I am checking using Project Fauxton.



{"selector": {"$and": [{"transferReportID": {"$ne": null}},{"effectiveDateOfAction": {"$gt": "10-31-2017"}}]}}


Please help me getting the correct query.







couchdb couchdb-mango fauxton






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 7:25









Neil Lunn

96.8k22170180




96.8k22170180










asked Nov 20 at 7:24









Priyanka

87




87












  • "$gt": "10-31-2017" is a lexicographic string comparison, so 10-31-2018 will be greater than 01-01-2019. You may have to create a view, which emits your data sorted by date (see docs).
    – Enno
    Nov 20 at 9:40










  • @Enno Thank you for the reponse. I am new to couch DB query, If Possible can you please share the working example for this.
    – Priyanka
    Nov 20 at 11:33


















  • "$gt": "10-31-2017" is a lexicographic string comparison, so 10-31-2018 will be greater than 01-01-2019. You may have to create a view, which emits your data sorted by date (see docs).
    – Enno
    Nov 20 at 9:40










  • @Enno Thank you for the reponse. I am new to couch DB query, If Possible can you please share the working example for this.
    – Priyanka
    Nov 20 at 11:33
















"$gt": "10-31-2017" is a lexicographic string comparison, so 10-31-2018 will be greater than 01-01-2019. You may have to create a view, which emits your data sorted by date (see docs).
– Enno
Nov 20 at 9:40




"$gt": "10-31-2017" is a lexicographic string comparison, so 10-31-2018 will be greater than 01-01-2019. You may have to create a view, which emits your data sorted by date (see docs).
– Enno
Nov 20 at 9:40












@Enno Thank you for the reponse. I am new to couch DB query, If Possible can you please share the working example for this.
– Priyanka
Nov 20 at 11:33




@Enno Thank you for the reponse. I am new to couch DB query, If Possible can you please share the working example for this.
– Priyanka
Nov 20 at 11:33












2 Answers
2






active

oldest

votes


















0














As there is no native date type in JSON, it's important to store dates in a format that makes sense at query time. The "Month-Day-Year" format may be useful when rendering dates for a US audience but it makes little sense for querying.



I would suggest the "YYYY-MM-DD" format e.g "2018-10-30". This stores the same data as before but the sort order happens to be in date order, because years are longer than months and months are longer than days.



You can then use a query using the "$gte" operator:



{
"selector": {
"effectiveDateOfAction": {
"$gte": "2018-10-01"
}
}
}


This reads as "fetch documents whose 'effectiveDateOfAction' field is greater than or equal to 1st October 2018'.



See this blog post on how to store and query dates in CouchDB.






share|improve this answer





























    0














    If possible, change your date format to a sortable form, as Glyn Bird said. I'd suggest to use ISO_8601, this is preferd for JSON (e.g. Javascript Date.toJSON).



    If you can't change your data, you can create a view, which transforms your dates into a sortable format.



    Example: Put a design doc similar to the following to your database



    {
    _id: '_design/employees',
    views: {
    by_action_date: {
    map: "function (doc) {n if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docsn var dt = doc.effectiveDateOfAction.split('-'); // parse your date formatn emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as keyn }n }"
    }
    }
    }


    The map function has to be given as string in the document, formatted it is:



    function(doc) {
    if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docs
    var dt = doc.effectiveDateOfAction.split('-'); // parse your date format
    emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as key
    }
    }


    You can then query it to get your employees sorted:

    Use include_docs = true param to get your real documents included.



    /my-database/_design/employees/_view/by_action_date?include_docs=true


    You then can also use startkey and endkey params, to limit to a specific time frame:



    /my-database/_design/employees/_view/by_action_date?include_docs=true&startkey="2018-10-01"&endkey="2018-10-31"


    This will return your TRAN001 and TRAN002 documents.






    share|improve this answer























      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%2f53388100%2fcouch-db-query-to-fetch-the-records-greater-than-a-particular-date%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      As there is no native date type in JSON, it's important to store dates in a format that makes sense at query time. The "Month-Day-Year" format may be useful when rendering dates for a US audience but it makes little sense for querying.



      I would suggest the "YYYY-MM-DD" format e.g "2018-10-30". This stores the same data as before but the sort order happens to be in date order, because years are longer than months and months are longer than days.



      You can then use a query using the "$gte" operator:



      {
      "selector": {
      "effectiveDateOfAction": {
      "$gte": "2018-10-01"
      }
      }
      }


      This reads as "fetch documents whose 'effectiveDateOfAction' field is greater than or equal to 1st October 2018'.



      See this blog post on how to store and query dates in CouchDB.






      share|improve this answer


























        0














        As there is no native date type in JSON, it's important to store dates in a format that makes sense at query time. The "Month-Day-Year" format may be useful when rendering dates for a US audience but it makes little sense for querying.



        I would suggest the "YYYY-MM-DD" format e.g "2018-10-30". This stores the same data as before but the sort order happens to be in date order, because years are longer than months and months are longer than days.



        You can then use a query using the "$gte" operator:



        {
        "selector": {
        "effectiveDateOfAction": {
        "$gte": "2018-10-01"
        }
        }
        }


        This reads as "fetch documents whose 'effectiveDateOfAction' field is greater than or equal to 1st October 2018'.



        See this blog post on how to store and query dates in CouchDB.






        share|improve this answer
























          0












          0








          0






          As there is no native date type in JSON, it's important to store dates in a format that makes sense at query time. The "Month-Day-Year" format may be useful when rendering dates for a US audience but it makes little sense for querying.



          I would suggest the "YYYY-MM-DD" format e.g "2018-10-30". This stores the same data as before but the sort order happens to be in date order, because years are longer than months and months are longer than days.



          You can then use a query using the "$gte" operator:



          {
          "selector": {
          "effectiveDateOfAction": {
          "$gte": "2018-10-01"
          }
          }
          }


          This reads as "fetch documents whose 'effectiveDateOfAction' field is greater than or equal to 1st October 2018'.



          See this blog post on how to store and query dates in CouchDB.






          share|improve this answer












          As there is no native date type in JSON, it's important to store dates in a format that makes sense at query time. The "Month-Day-Year" format may be useful when rendering dates for a US audience but it makes little sense for querying.



          I would suggest the "YYYY-MM-DD" format e.g "2018-10-30". This stores the same data as before but the sort order happens to be in date order, because years are longer than months and months are longer than days.



          You can then use a query using the "$gte" operator:



          {
          "selector": {
          "effectiveDateOfAction": {
          "$gte": "2018-10-01"
          }
          }
          }


          This reads as "fetch documents whose 'effectiveDateOfAction' field is greater than or equal to 1st October 2018'.



          See this blog post on how to store and query dates in CouchDB.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 at 11:00









          Glynn Bird

          3,7451817




          3,7451817

























              0














              If possible, change your date format to a sortable form, as Glyn Bird said. I'd suggest to use ISO_8601, this is preferd for JSON (e.g. Javascript Date.toJSON).



              If you can't change your data, you can create a view, which transforms your dates into a sortable format.



              Example: Put a design doc similar to the following to your database



              {
              _id: '_design/employees',
              views: {
              by_action_date: {
              map: "function (doc) {n if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docsn var dt = doc.effectiveDateOfAction.split('-'); // parse your date formatn emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as keyn }n }"
              }
              }
              }


              The map function has to be given as string in the document, formatted it is:



              function(doc) {
              if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docs
              var dt = doc.effectiveDateOfAction.split('-'); // parse your date format
              emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as key
              }
              }


              You can then query it to get your employees sorted:

              Use include_docs = true param to get your real documents included.



              /my-database/_design/employees/_view/by_action_date?include_docs=true


              You then can also use startkey and endkey params, to limit to a specific time frame:



              /my-database/_design/employees/_view/by_action_date?include_docs=true&startkey="2018-10-01"&endkey="2018-10-31"


              This will return your TRAN001 and TRAN002 documents.






              share|improve this answer




























                0














                If possible, change your date format to a sortable form, as Glyn Bird said. I'd suggest to use ISO_8601, this is preferd for JSON (e.g. Javascript Date.toJSON).



                If you can't change your data, you can create a view, which transforms your dates into a sortable format.



                Example: Put a design doc similar to the following to your database



                {
                _id: '_design/employees',
                views: {
                by_action_date: {
                map: "function (doc) {n if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docsn var dt = doc.effectiveDateOfAction.split('-'); // parse your date formatn emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as keyn }n }"
                }
                }
                }


                The map function has to be given as string in the document, formatted it is:



                function(doc) {
                if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docs
                var dt = doc.effectiveDateOfAction.split('-'); // parse your date format
                emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as key
                }
                }


                You can then query it to get your employees sorted:

                Use include_docs = true param to get your real documents included.



                /my-database/_design/employees/_view/by_action_date?include_docs=true


                You then can also use startkey and endkey params, to limit to a specific time frame:



                /my-database/_design/employees/_view/by_action_date?include_docs=true&startkey="2018-10-01"&endkey="2018-10-31"


                This will return your TRAN001 and TRAN002 documents.






                share|improve this answer


























                  0












                  0








                  0






                  If possible, change your date format to a sortable form, as Glyn Bird said. I'd suggest to use ISO_8601, this is preferd for JSON (e.g. Javascript Date.toJSON).



                  If you can't change your data, you can create a view, which transforms your dates into a sortable format.



                  Example: Put a design doc similar to the following to your database



                  {
                  _id: '_design/employees',
                  views: {
                  by_action_date: {
                  map: "function (doc) {n if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docsn var dt = doc.effectiveDateOfAction.split('-'); // parse your date formatn emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as keyn }n }"
                  }
                  }
                  }


                  The map function has to be given as string in the document, formatted it is:



                  function(doc) {
                  if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docs
                  var dt = doc.effectiveDateOfAction.split('-'); // parse your date format
                  emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as key
                  }
                  }


                  You can then query it to get your employees sorted:

                  Use include_docs = true param to get your real documents included.



                  /my-database/_design/employees/_view/by_action_date?include_docs=true


                  You then can also use startkey and endkey params, to limit to a specific time frame:



                  /my-database/_design/employees/_view/by_action_date?include_docs=true&startkey="2018-10-01"&endkey="2018-10-31"


                  This will return your TRAN001 and TRAN002 documents.






                  share|improve this answer














                  If possible, change your date format to a sortable form, as Glyn Bird said. I'd suggest to use ISO_8601, this is preferd for JSON (e.g. Javascript Date.toJSON).



                  If you can't change your data, you can create a view, which transforms your dates into a sortable format.



                  Example: Put a design doc similar to the following to your database



                  {
                  _id: '_design/employees',
                  views: {
                  by_action_date: {
                  map: "function (doc) {n if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docsn var dt = doc.effectiveDateOfAction.split('-'); // parse your date formatn emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as keyn }n }"
                  }
                  }
                  }


                  The map function has to be given as string in the document, formatted it is:



                  function(doc) {
                  if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docs
                  var dt = doc.effectiveDateOfAction.split('-'); // parse your date format
                  emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as key
                  }
                  }


                  You can then query it to get your employees sorted:

                  Use include_docs = true param to get your real documents included.



                  /my-database/_design/employees/_view/by_action_date?include_docs=true


                  You then can also use startkey and endkey params, to limit to a specific time frame:



                  /my-database/_design/employees/_view/by_action_date?include_docs=true&startkey="2018-10-01"&endkey="2018-10-31"


                  This will return your TRAN001 and TRAN002 documents.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 29 at 8:42

























                  answered Nov 28 at 16:03









                  Enno

                  1117




                  1117






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53388100%2fcouch-db-query-to-fetch-the-records-greater-than-a-particular-date%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

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga