Retrieve mongodb objects that fall within a specified date range

Within my collection, there is an example document structured as follows:

{
    "_id" : ObjectId("5bbb299f06229dddbaab553b"),
    "phone" : "+38 (031) 231-23-21",
    "date_call" : "2018-10-08",
    "adress_delivery" : "1",
    "quantity_concrete" : "1",
    "state" : "200",
    "comments" : "1",
    "is_order" : "n",
    "date_delivery" : "",
    "quantity_orders" : "",
    "summ_order" : "",
    "profit" : "",
    "id" : "0"
}

My objective is to filter data within a specified date range, for instance, between 2018-01-08 and 2018-10-10 :

  • Total number of documents

  • Number of documents where "is_order": "y"

I would appreciate your guidance on the most efficient method for filtering this data.

  • Should I load all the data first from AJAX response and then filter it?
  • Is it preferable to filter the data entirely on the server? If so, can it be accomplished in one request or should I split the request, create an array, and send a response?

Answer №1

(1) Find the number of records between January 8, 2018 and October 10, 2018

Model.find({
      "date_call":{
                  "$lte":"2018-10-10",
                  "$gte":"2018-01-08"
                  }
     },function(err,data){
     if(err){ return res.status(500).json({status:'error',errors:err});}
     if(!err && data){ return res.status(200).json({stauts:'success',data:data});}
});

(2) Retrieve the count of documents with the value "is_order": "y"

Model.find({
          "is_order": "y",
          "date_call":{
                      "$lte":"2018-10-10",
                      "$gte":"2018-01-08"
                      }
         },function(err,data){
         if(err){ return res.status(500).json({status:'error',errors:err});}
         if(!err && data){ return res.status(200).json({stauts:'success',data:data});}
    });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using a git repository as a dependency in your project and then importing the dependency

Struggling to integrate a git repository as a component has been my challenge for the past day. Despite trying multiple approaches, none of them seemed to work when I ran npm i. Here are some of the methods I attempted: npm install from Git in a specific ...

When utilizing a third-party library in conjunction with Vuetify, the V-menu may have a tendency to automatically close right after

Incorporating cytoscape into a vuetify SPA has been successful for the most part. The graph renders within a v-card-element, and I can navigate to a different page using the vue router when clicking a note in the graph. However, when attempting to replace ...

Leveraging the Spread Operator in Redux mapDispatchToProps with SweetAlert2

When I add the swal action creators to the mapDispatchToProps function like this: function mapDispatchToProps(dispatch) { return { getAnimal: (_id) => dispatch(getAnimal(_id)), ...swal } } The issue aris ...

Unable to set values to an array of objects in JavaScript

Currently, I am facing an issue in my node.js project where I am unable to assign values to an array of objects. This problem has occurred before, but I just can't seem to figure out the root cause. My suspicion is that it might be related to variable ...

What is the reason for node executing both of these handlers on the server?

My node app is set up with two handlers, one for requests to the root URL and another for the slug. However, when I visit the root or slug URLs, the app crashes and I receive this error: url is pointing to root url has a slug throw err; // Rethrow non-MySQ ...

Managing several drop elements using class dynamically

I am facing a challenge in combining drag and drop listeners into one function to create dynamic zones with the same class. This will allow me to call a single function for uploading files. The upload function is currently working correctly, but I need a ...

Adding two input values using jQuery

Here is the function compute() that I am working with: function compute() { if ($('input[name=type]:checked').val() != undefined) { var a = $('input[name=service_price]').val(); var b = $('input[name=modem_pric ...

Exploring the power of Angular's ng-repeat to generate hierarchical lists

I am looking to display a structured list of layers and sublayers by looping through an object using ng-repeat. var lyrslist = [ { layername: "Base Maps", layertype: "layer" }, { layername: "Open Street Map", layertype: "sublayer" },    { layer ...

Verify whether the element retains the mouseenter event after a specified delay

I recently implemented some blocks with a mouseenter and mouseleave event. <button onMouseEnter={this.MouseEnter}>hover</button> MouseEnter(e) { setTimeout(() => { // Checking if the mouse is still on this element // Pe ...

Examining a Grunt task: Insights and effective testing techniques

I could use some guidance on creating a complex Gruntfile.js and incorporating it with tests. Am I approaching Grunt correctly? I'm seeking input from the community to assist me and contribute in a meaningful way. I am developing a new task for Grunt ...

Having trouble adding or upgrading packages for your React project?

Struggling with my React project as every time I try to update or install new packages, my computer starts making a lot of noise. It seems to be related to the versions of react-dom and react-router-dom, but I'm unsure how to resolve this without caus ...

Obtaining the responseJSON property from a jQuery $.ajax object involves accessing the data returned

Recently, I encountered an issue with my JavaScript code that involves an AJAX request: $ajax = $.ajax({ type: 'GET', url: 'DBConnect.php', data: '', dataType: 'json', success: function(data) {} ...

The AWS serverless-express deployed via AWS API Gateway is unexpectedly requesting authentication despite not being configured for it

Apologies for my novice question, but I am still getting the hang of AWS, NodeJS, and Express. I am in the process of setting up a simple serverless API Gateway: index.js import AwsServerlessExpress from 'aws-serverless-express'; import App fr ...

Looking to adjust the title font size when exporting DataTable to Excel?

Would like to customize the title of an excel file exported from Datatable. I attempted to implement a solution found on a stackoverflow post, but it ended up applying the customization to the entire sheet. $("#datatable").DataTable({ ...

Having issues retrieving a JSON array in PHP with the json_decode function

Can someone assist me with passing and returning an array to a PHP script? I have successfully tested the json_encode portion, but I am facing issues with the json_decode on the PHP side. Javascript scid_list = []; $('.filter_on').each ...

ES6 Promises have a curious ability to accurately retrieve values from custom JavaScript objects

I have developed a unique library that functions independently from the Promise API, yet achieves similar objectives. It utilizes window.requestAnimationFrame and fallbacks to setTimeout, having no similarities with Promises. Interestingly, it is compatibl ...

Unraveling the mystery: How does JavaScript interpret the colon?

I have a quick question: When I type abc:xyz:123 in my GoogleChrome browser console, it evaluates to 123. How does JavaScript interpret the : symbol in this scenario? ...

The Discord.js command outright declines to function

I'm having trouble with a code that I'm working on. The goal is to create a command that is enabled by default, but once a user uses it, it should be disabled for that user. However, when I try to execute the code, it doesn't work at all and ...

Recompiling Node.js/npm packages after copying an app

Scenario: In the setup, there is a virtual machine with internet access where the command npm install is run to install app dependencies. This results in a folder containing the app and its dependencies in node_modules. One of the app modules is mong ...

prevent parent jQuery functions from interfering with child function execution

I am facing an issue with conflicting jQuery functions between parent and child elements. The child function is a bootstrap function, while the parent is a custom function. The main objective is to limit the height of the parent div (refer to the code belo ...