Navigating and exploring data stored in a mongoose database

Currently, I am attempting to filter data based on a specific id (bWYqm6-Oo) and dates between 2019-09-19 and 2019-09-22. The desired result should return the first three items from the database. However, my current query is returning an empty array. I would appreciate any assistance in resolving this issue. Thank you!

var findBy = {
    _id : bWYqm6-Oo,
    exercises : [
      {
        date: {
          $gte: 2019-09-19,
          $lt: 2019-09-22
        }
      }
    ]
  }
  UserModel.find(findBy).limit(5).exec((err, data) => {
      (err) ? res.json({"error" : "problem searching for exercises: " + err}) :  res.json(data);      
  });

The structure of my database:

{
    _id: "bWYqm6-Oo",
    user: "rommy",
    exersises: [
        {
        user_id: "bWYqm6-Oo",
        date: "2019-09-20T00:00:00.000Z",
        description: "stiup",
        duration: "22",
        _id: "pu90D-dHx"
        },
        {
        user_id: "bWYqm6-Oo",
        date: "2019-09-21T00:00:00.000Z",
        description: "pushup",
        duration: "22",
        _id: "YtfsJLOXb"
        },
        {
        user_id: "bWYqm6-Oo",
        date: "2019-09-20T00:00:00.000Z",
        description: "stiup",
        duration: "22",
        _id: "pu90D-dHx"
        },
        {
        user_id: "bWYqm6-Oo",
        date: "2019-09-24T00:00:00.000Z",
        description: "stiup",
        duration: "22",
        _id: "pu90D-dHx"
        }
    ],
    __v: 9
}

In my server.js file, here is where I call my files:

Answer №1

Just a few points to consider:

In MongoDB, the _id field for a document is always unique, so including it in your query like this:

var findBy = {
    _id : bWYqm6-Oo,
    exercises : [
      {
        date: {
          $gte: 2019-09-22,
          $lt: 2019-09-22
        }
      }
    ]
  }

The section regarding the 'exercises' array with a date range filter seems unnecessary since there will only be one document with the _id of 'bWYqm6-Oo'.

If you want to query based on a date range, you can simplify it by using a structure like this:

var query = {
  'exercises.date': {$gte: '2019-09-22', $lt: '2019-09-22'}
}

I hope this clarifies things for you.

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

Launching a modal by passing data in an MVC environment

I am struggling with a task where I have a list of objects, and upon clicking on any of them, I want a modal to open displaying the data of the clicked object. Despite thinking it would be simple, I just can't seem to get it to work. My goal is to pas ...

The Outer Div Can't Contain Google Maps

I am currently working on integrating a map widget into a dashboard I created using gridstack.js. The sample widget layout that I am aiming for can be seen in the link below: https://i.sstatic.net/ZQP6G.png My goal is to embed the map within the inner (w ...

Vue.js encountering an issue of receiving null for the JSON data upon the initial loading phase

Currently, I am expanding my knowledge on vue.js and experimenting with calling a json file to parse the data. Although everything seems to be functioning as intended, whenever I refresh the page, there is a momentary blank screen before the data loads. In ...

javascript problem with class method for loading json file

I've encountered an issue with my class setup below. Despite most things working, the console keeps throwing an error when json.onload is triggered. The error message reads "Uncaught TypeError: Cannot read property of 'push' of undefined". ...

Switch the background color alternately from red to green every second

Need help with a webpage that changes the background color every second using JavaScript. The issue lies in figuring out how to correctly change the variable within the function. Here's an example of the code: <!DOCTYPE html> <html> ...

Is there a way to identify the browser version that is being used?

I've been on the lookout for a specific code snippet that can help me identify whether the user visiting my website is using Firefox 3 or 4. Thus far, I have only come across code to determine the type of browser without indicating the version. Is th ...

Limit the API call to only respond to requests from the localhost

I'm currently working on a website that uses API calls within the same node project. I would like to restrict most of these API calls to only be accessible by the localhost website. Is there a way to achieve this without implementing OAuth and simply ...

Determining the spatial capacity of a mesh using ThreeJS surpasses the volume of its bounding box

Issue at Hand: The challenge lies in the fact that the bounding box volume is turning out to be smaller than the volume calculated from the mesh. Attempts So Far: To begin with, I computed the volume of a bounding box using the following code: //loaded ...

Navigating through a React application with several workspaces - the ultimate guide

Currently, I am working on implementing a monorepo setup inspired by this reference: https://github.com/GeekyAnts/nativebase-templates/tree/master/solito-universal-app-template-nativebase-typescript In this repository, there are 4 distinct locations wher ...

A difference in the way content is displayed on Firefox compared to Chrome, Edge, and Safari

Recently, I encountered an issue with a tool I had developed for generating printable images for Cross-Stitch work. The tool was originally designed to work on Firefox but is now only functioning properly on that browser. The problem at hand is whether th ...

Error: The function m.easing[this.easing] is not defined

I have been working on creating anchor link scrolling and tooltip display using bootstrap, but I am encountering an issue. $(window).scroll(function(){ if ($(window).scrollTop() >= 100) { $('#header').addClass('fixed'); ...

Troubleshooting Node app on AWS with Docker exit status 1

I've been running a beta app on AWS with Express.js, Node, mongoose, and docker. With less than 10 daily active users, mainly friends helping test it out, the app seems to go down almost every day. At first, I suspected an issue with AWS itself, so I ...

The server's response contained a MIME type of "application/octet-stream" that did not include JavaScript. Module scripts in HTML are subject to strict MIME type checking

Here is the structure of my project: node_modules server.js public: glsl: fragment.glsl vertex.glsl index.html main.js img.jpg style.css I have set up a simple server to serve a three.js animation in server.js const express = require('expre ...

Create a dynamic menu dropdown with absolute positioning using React

I recently made the transition to React and now I am in the process of converting my old vanillaJS website into ReactJS. One issue I am facing is with a button that is supposed to trigger the opening of a dropdown menu. <button type="button&qu ...

Problem encountered with AngularJS html5mode URL functionality

I am encountering an issue with my AngularJS application that does not contain any nodeJS code. The problem lies in removing the # from the URL and I have implemented ui-routes for routing. 'use strict'; var app = angular.module('myapp&apos ...

Tips for sending multiple values in a data object using jQuery AJAX

I am currently working on a form that contains two input fields, with the possibility of more being added later. The first input is a text field and the second is a checkbox. I want to be able to send these inputs using $.ajax. To accomplish this, I have ...

What causes Gun.js to generate duplicate messages within a ReactJs environment?

I need assistance with my React application where gun.js is implemented. The issue I am facing is that messages are being duplicated on every render and update. Can someone please review my code and help me figure out what's wrong? Here is the code s ...

Managing multiple instances of express in a node.js application

After coming into possession of a Node.js codebase, I found myself grappling with the following snippets: var app1 = express(); app1.listen(8080) var app2 = express(); app2.listen(8081) var app3 = express(); app3.listen(8082) All of these lines reside ...

How to properly format credit card input using JavaScript or jQuery

I need to implement a feature where users input their credit card information, and once they complete the form, I want to display the credit card number in this format: xxxxxxxxxxxx1111 or xxxx-xxxx-xxxx-1111, without altering the actual input value. Is ...

What is the best way to choose dropdown values by utilizing various button IDs?

I have four different vacation destinations and four buttons. I want to automatically select each destination separately when the corresponding button is clicked. <select class="aa" required="" name="f1990" {input.multiple}="" {input.size}="" id="f19 ...