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

  • SEARCH
  • Home
  • 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 8796963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:44:41+00:00 2026-06-13T23:44:41+00:00

I want to combine two OR-queries with AND in Monoose, like in this SQL

  • 0

I want to combine two OR-queries with AND in Monoose, like in this SQL statement:

SELECT * FROM ... WHERE (a = 1 OR b = 1) AND (c=1 OR d=1)

I tried this in a NodeJS module which only gets the model object from the main application:

/********** Main application ***********/
var query = MyModel.find({});
myModule1.addCondition(query);
myModule2.addCondition(query);
query.exec(...)

/************ myModule1 ***************/
exports.addCondition = function(query) {
  query.or({a: 1}, {b: 1});
}

/************ myModule2 ***************/
exports.addCondition = function(query) {
  query.or({c: 1}, {d: 1});
}

But this doesn’t work, all OR-conditions will get joined together like in this SQL statement:

SELECT * FROM ... WHERE a = 1 OR b = 1 OR c=1 OR d=1

How can I combine the two conditions of myModule1 and myModule2 with AND in Mongoose?

  • 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-13T23:44:42+00:00Added an answer on June 13, 2026 at 11:44 pm

    It’s probably easiest to create your query object directly as:

      Test.find({
          $and: [
              { $or: [{a: 1}, {b: 1}] },
              { $or: [{c: 1}, {d: 1}] }
          ]
      }, function (err, results) {
          ...
      }
    

    But you can also use the Query#and helper that’s available in recent 3.x Mongoose releases:

      Test.find()
          .and([
              { $or: [{a: 1}, {b: 1}] },
              { $or: [{c: 1}, {d: 1}] }
          ])
          .exec(function (err, results) {
              ...
          });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is for Android SQLite. I have two queries like this: select * from
how can i combine these two queries in mysql? select count(*) as entry_count from
I have two queries which i would like to combine There are 4 tables
I have two sql queries... set @count:=0; select @count:=@count+1 as SNO, col1, col2 FROM
i have two sql queries. select EmployeeID,FName from [Mydatabase].[dbo].EMPLOYEE_TABLE where EmployeeID not in (select
I want to combine these two queries into one: mysql_query(INSERT INTO categories (name, parent)
I have a database for a Train company. I want to combine two queries
I have two queries I want to combine into a single output, I can't
I want to learn how to combine two db tables which have no fields
I want to combine the two following queries, can anyone give me a idea

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.