Locate the closest match with Mongoose and MongoDB

I am currently working with mongoose and have a model called tickets.

The model includes instructions for: Fromticket, Number default 0 Toticket, number, default 0

For example, if I have the following rows:

{fromticket:1 , toticket:3000},
{fromticket:3001 , toticket:29000}

,

How can I search for the row that contains a specific ticket number such as 20000 or 3001? (In this case, it would be row number 2)

Tickets.find( row where owner is ticket 2000)

Answer №1

When executing a query, keep in mind that each term is automatically combined with AND logic. For example, to find documents where the value of fromticket is less than or equal to 20000 and the value of toticket is greater than or equal to 20000, you can use the following code:

var ticketValue = 20000;
Tickets.findOne({
    fromticket: {$lte: ticketValue},
    toticket: {$gte: ticketValue}
}, (error, result) => {...});

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

Is it possible to apply capitalization or convert the value of props to uppercase in React?

Despite attempting the toUpperCase method, I am unable to capitalize my props value. Here's the code I have: export default function Navbar(props) { return ( <> <div> <nav class="navbar navbar-expand-lg bg-b ...

The path alias feature in Sveltekit seems to be malfunctioning

I've been working with Svelte Kit (using TypeScript) and I'm having trouble getting the new link "$base" to work for my shortlinks. I've added the shortlink information below: ./jsconfig.json { "compilerOptions": { "mod ...

Virtual machines have encountered issues when attempting to utilize setTimeout within the browser with vm.runInNewContext

When running a JS script using the vm module in a browser, the following details are included: vm.runInNewContext(codeToEval, sandboxObject); Although interval methods like setTimeout and setInterval do not work, even when exposed in the sandboxObject cr ...

Utilizing the nested combination of map and filter functions

Struggling to grasp the concept of functional programming in JavaScript, my aim is to extract object boxart items with width=150 and height=200. Console.log(arr) the output shows [ [ [ [Object] ], [ [Object] ] ], [ [ [Object] ], [ [Object] ] ] ] I&apos ...

"Mastering the art of crafting a between clause in MongoDB using the Mongo Mapper tool

I have been attempting to construct a query in this manner User.find_each(created_at: [1.day.ago.utc, Date.now]) do |user| However, it was unsuccessful. It consistently returns 0 users, even though I am certain that there are users created within the pa ...

PHP and JavaScript Form: Include the page URL in the email form when submitting

After submitting a basic form, I am struggling to include the URL of the page in the email notification to track where the user completed the form. I have attempted various solutions from this forum, but unfortunately none of them seem to work. How can I ...

Tips for reversing a sketch: Creating a timer where the text continuously refreshes causing it to intersect

Currently, I am working on developing a stopwatch that is functional. However, I am facing an issue where the text overlaps itself when it changes due to repetitive drawing. Removing the strokeText and fillText from the interval prevents it from changing a ...

click event to activate delayed function in dropdown

Within the li element, there is an onclick function with a data-toggle="dropdown" attribute. The issue at hand is that my function isn't triggered when I click once, but interestingly it works after clicking twice. https://i.sstatic.net/GdvoT.png I ...

ID could not be retrieved from the checkbox

I am facing an issue with my HTML checkboxes. The ids are generated from an angular ng-repeat, but when I try to collect the id for use, it always returns as undefined. $("input:checkbox.time-check-input").each(function () { var ruleUnformatted = ""; ...

How to retrieve the penultimate document in MongoDB using Mongoose?

Is it possible to retrieve the document collection from the second to latest or previous day using Mongoose? My current code retrieves the latest data as shown below: data.findOne() .sort({"_id": -1}) .exec(function(err, data) { ...

Error when spaces are present in the formatted JSON result within the link parameter of the 'load' function in JQuery

I am facing an issue with JSON and jQuery. My goal is to send a formatted JSON result within the link using the .load() function. <?php $array = array( "test1" => "Some_text_without_space", "test2" => "Some text with space" ); $json = jso ...

Creating an object in JavaScript using an array type初始化数组类型为对象javascript

In my code, there is an interface defined as Products export interface Products{ category: string; imageUrl: string; price: number; title: string; } Within my component, I have a variable named products which is an array of type Product ...

Here are the steps to calculate the duration between two dates in the specified format:

let d1 = new Date("05/20/2022 09:28:15") let d2 = new Date("05/24/2022 12:38:25") Can someone help me calculate the difference between these two dates? ...

Steps to extract selected values from the MUI data grid

Can values be retrieved from a mui DataGrid? I have a table and I would like to create a custom export that takes into account user filters and the display status of columns. However, I need to access these filtered values. Is there a way to achieve this ...

Is there a way to verify if an email is already registered within a MERN stack application

I am in the process of creating a registration form and need to verify if an email already exists within the system. Below is the React code snippet showcasing the structure for better understanding. In the schema, emails are defined as unique. AuthContr ...

Utilizing the initial entry in MongoDB exclusively

I have developed a unique adventure command that searches for a player's profile in a Mongo database to calculate various game metrics such as xp and coins. While my other database commands are working flawlessly, this particular command is causing an ...

The improper utilization or replacement of Jest mock in an Angular standalone component's unit test is causing issues

Presented here is a streamlined Ionic/Angular component with unnecessary code removed. import { IonicModule, ModalController } from '@ionic/angular'; @Component({ selector: 'my-component', templateUrl: 'my-component.html' ...

The issue of PassportJS Local Strategy not functioning properly when integrated with MongoDB

Struggling to implement user authentication with PassportJS, I'm facing challenges related to my database setup. Utilizing MongoDB without Mongoose, LocalStrategy module seems to be causing issues that I can't seem to resolve. Trying to keep my ...

Transferring information to a server with node.js

I have implemented AJAX to send data to the server. The code snippet I am using is: function post(url) { return new Promise(function(resolve, reject) { var req = new XMLHttpRequest(); req.open('POST', url); req.onload = function() ...

Utilize @db.Decimal within the Prisma framework for the parameters "s", "e", and "d"

When I define the schema in Prisma like this: value Decimal? @db.Decimal(7, 4) Why do I always get this format when retrieving the value from the database: "value": { "s": 1, "e": 0, & ...