Data not being retrieved by Meteor.js / MongoDB query for date range

Here is the code snippet I am using to retrieve data from a Mongo Collection:

var currentDate = moment().toISOString();
// Returns: 2016-12-10T20:36:04.494Z

var futureDate = moment().add(10, "days").toISOString();
// Returns: 2016-12-20T20:36:04.495Z

return agenda = Agendas.find({
  "agendaDate": { '$gte': currentDate, '$lte': futureDate }
});

The date in the MongoDB Collection is stored as shown below:

{ 
"_id" : ObjectId("584877e56466dd236cd95f15"), 
"agendaDate" : ISODate("2016-12-12T17:28:25.000+0000"), 
"agendaTime" : "20:59", 
"agendaEvent" : "Test event" 
}

Despite setting up test documents, no results are being returned. There are 2 documents within the specified range, and 1 outside the range.

I need help understanding what is going wrong and correcting the code. Can anyone provide assistance?

Answer №1

In order to accurately compare dates, it is important to use actual date objects instead of strings that represent them.

This means extracting the date from your moment objects by utilizing the toDate() method.

var futureDate = moment().add(10, "days").toDate();

Answer №2

It's worth noting that moment.toISOString() will give you a string output, making it unsuitable for direct comparison with a date object in your MongoDB query. To address this issue, I recommend creating a separate date object instead. Best regards, Yann

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

Howler.js is giving an error message: "When creating a new Howl object, make sure to include an array of source files."

Trying to test Howler.js for playing audio files. When I click the button in this HTML file, there's an error in the console indicating that "An array of source files must be passed with any new Howl." Here is the code: <!DOCTYPE html> <htm ...

jQuery plugin stops functioning properly following the use of the jQuery display block method

In my project, I am exploring the use of divs as tabs with jQuery. Within these divs, I also want to incorporate another jQuery plugin. Currently, I have manually created these div tabs using jQuery and set the default style for second and subsequent divs ...

"Executing ng-click directive on a second click changes the route in AngularJS

Why does my ng-click only redirect to a new location on the second click? It should redirect to $location.path('/login.signin');, but it only works if I click the button again. It's strange. Where could this issue be originating from? Belo ...

Loading Animation for Pages and Tables

I'm experiencing a choppy and sudden transition when switching between two pages that contain tables populated using ng-repeat. Upon loading, the footer is positioned halfway up the page below the table heading until the table loads and the layout adj ...

The nested directive link function failed to execute and the controller was not recognized

Apologies in advance for adding to the sea of 'mah directive link function isn't called!' posts on Stack Overflow, but none of the solutions seem to work for me. I have a directive named sgMapHeader nested inside another directive called sg ...

Utilize a Google Chrome extension to interact with the DOM of the active tab

Alright, so I've got a good grasp on JavaScript/jQuery and can use them for various tasks. However, my current challenge involves manipulating the DOM of the open tab. I'm curious to know if it's feasible or if all the actions performed by a ...

How can I align rectangles with different heights to display side by side using Javascript?

Currently, I am designing a press page for a website where the headlines/articles are displayed in rectangles. To achieve this layout, I am using the following CSS: .press-blocks{ column-count: 4; column-gap: 2em; padding-left: 10%; padding ...

The Precision of the IRR (Internal Rate of Return) Calculation in Javascript

I've been working on a custom IRR function in JavaScript to mimic the functionality of Excel's IRR function. Despite my best efforts, it seems that my results are slightly off. Below is the code snippet that I have been using: var IRRval = []; ...

Traverse JSON data with JavaScript/jQuery

I am looking to iterate through the JSON data provided below using JavaScript. { "jsonUrl": "/testUrl", "data": { "country":"US", "company":"ABC", "items":[ { "id": "1", "i ...

The Material-UI DataGrid feature allows for the display of column sums, with the sum dynamically updating based on applied filters

I am struggling with calculating the sum of values in the Total Amount column of my Material-UI DataGrid. How can I achieve this and ensure that the sum is also updated when a filter is triggered? Any guidance on how to sum the entire Total Amount column ...

Leaving out the return statement in an asynchronous typed function

Consider this example: export async function foo(): Promise<string>{ await bar() return; } No errors are thrown during compilation. However, if you change the code to: export async function foo(): Promise<string>{ await bar() } You w ...

When attempting to print the content inside a Bootstrap modal, the display does not appear but instead a blank page is printed

My goal is to print the text content of a bootstrap modal while leaving space for images blank when clicking the print button located within the modal. Unfortunately, upon clicking the print button, I am only getting a blank page without any content displ ...

Unable to access a PHP file within a JavaScript loop

In my HTML document, there is a button with an onclick method inside a JavaScript <script> tag. When this button is clicked, it triggers the deleteRow function, which is responsible for removing a row from a table on the page. Within the deleteRow f ...

Angular Checkbox Binding Issue Detected

Struggling with a problem related to an Angular checkbox form that is generated from an API. I am able to create checkboxes, but encountering two issues: The checked state of the boxes does not align with the values retrieved from the API, and when I clic ...

Update the numerical data within a td element using jQuery

Is there a way to use jquery to increase numerical values in a <td> element? I've attempted the following method without success. My goal is to update the value of the td cell by clicking a button with the ID "#increaseNum". Here is the HTML st ...

Encountering the error message: "TypeError [ERR_INVALID_ARG_TYPE]: The initial parameter should either be a string, Buffer instance, or Uint8Array."

Having trouble with the payment gateway API and subscription creation. Encountering an error that I can't seem to resolve even after debugging. Despite my best efforts, the error persists. The form and everything else seem to be in order, but the err ...

Angular: handling asynchronous errors when no promise is utilized within a subscription

I am currently working with a material design table and have created custom functions to load the data and extract objects from a JSON array object. Here is a snippet of the code I am using: public getDocumentList() { return this.http.get(this.getDocu ...

Subclassing Observable in RxJS 5: Parent class instances are returned by static methods

I've been experimenting with subclassing the RxJS Observable class and overriding the lift method, as explained here. While I can successfully add new operators to the prototype, when trying to create a new Observable using my subclass (such as MyObs ...

Unable to dynamically insert values into a JSON object

My goal is to populate a JSON object with objects that look like this: var books = [{ "no" : 1, "bookType":"fiction", "bookPrice": 60 },{ "no" : 2, "bookType":"f ...

Is there a way to verify if a date falls within the current week using javascript?

Is the date "2016-04-23T11:45:00Z" within this current week? Appreciate your help, ...