Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:15:53+00:00 2026-06-15T08:15:53+00:00

I am working on a nodejs/express app with Mongodb on the backend. In one

  • 0

I am working on a nodejs/express app with Mongodb on the backend. In one of my API calls, depending on the presence of a particular querystring parameter or the other I want to issue a query to Mongodb with either a $gt or a $lt.

In some cases we want to ask for everything less than the tokenId using $lt, but in other cases we want everything greater than the tokenId using $gt. How do we do that without duplicating the queries?

Here’s an example query:

collection.find({'film_id': {$in : genre}, '_id': {$lt: tokenId}}).sort({'_id': -1}).limit(25).toArray(function(error, films)

Is there a way to dynamically create the query without actually doing 2 different queries?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:15:54+00:00Added an answer on June 15, 2026 at 8:15 am

    Build up your query object programmatically:

    var query = {'film_id': {$in : genre}};
    if (param) {
        query._id = {$lt: tokenId};
    } else {
        query._id = {$gt: tokenId};
    }
    collection.find(query).sort({'_id': -1}).limit(25).toArray(function(error, films);
    

    Update

    Now that Node.js 4+ supports computed property names, you can create query in one step as:

    var query = {
        film_id: {$in: genre},
        _id: {[param ? '$lt' : '$gt']: tokenId}
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a RESTful api built with NodeJS, express, express-resource, and sequelize for
For the past 6 months, I've been working on my own nodeJS app, where
Currently I am working on a nodejs project. I have installed Express 3.X which
I am currently working on a project with 3 friends using nodeJs, expressJs, MongoDB,
I have the usual nodejs express app... var express = require('express'); var app =
I'm working on my first node.js / express / mongoose app and I'm facing
My node.js app uses express, socket.io and talks to mongodb through mongoose. All these
I'm working my way through an example using Nodejs & Express. When trying it
I have recently been working on a nodejs app which uses open socket connections
I'm working on a small NodeJS app that uses everyauth (technically mongooseAuth) to talk

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.