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 7043283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:16:13+00:00 2026-05-28T02:16:13+00:00

I am trying to perform a query which is composed of two $or ‘s:

  • 0

I am trying to perform a query which is composed of two $or‘s:

|--------------------
| Date1  |  Date2   |
|--------------------
| NULL   |  NULL    | *
| NULL   |  TODAY   | *
| NULL   |  TOMRW   | 
| TODAY  |  TODAY   | *
| TODAY  |  NULL    | *
| TOMRW  |  NULL    | 
|--------------------

(I’ve marked the rows that would match with an asterisk)

(Date1 == null || Date1 <= today) && (Date2 == null || Date2 <= today)

I am not sure how to express this query in MongoDB.

It can be broken down into two individual queries that do exactly what they should:

{
    "$or": [{
        "Date1": {
            "$exists": false
        }
    },
    {
        "Date1": {
            "$exists": true,
            "$lte": new Date("2012-01-07T04:45:52.057Z")
        }
    }]
}

and

{
    "$or": [{
        "Date2": {
            "$exists": false
        }
    },
    {
        "Date2": {
            "$exists": true,
            "$lte": new Date("2012-01-07T04:45:52.057Z")
        }
    }]
}

Both of these select the correct set of documents – I just dont know how to execute them as a single query.

My initial thought was to do a query like this:

{
    $and: [orQuery1, orQuery2]
}

Using an $and query returns 0 results. It was explained why here in this thread: $and query returns no result

Also in that thread, a suggestion was made to do a query like this:

{
    Key: {valToMatch1: 1, valToMatch2: 2}
}

But I dont think an $or can be executed this way.

So, the question is: How do I construct my query such that I can combine the two $or’s into a single query?

(Its getting very late so I hope this question makes sense.)

  • 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-05-28T02:16:14+00:00Added an answer on May 28, 2026 at 2:16 am
    use test
    db.test.insert({a:1})
    db.test.insert({a:2, Date2:new Date("01/07/2012")})
    db.test.insert({a:3, Date2:new Date("01/08/2012")})
    db.test.insert({a:4, Date1:new Date("01/07/2012"), Date2:new Date("01/07/2012")})
    db.test.insert({a:5, Date1:new Date("01/07/2012")})
    db.test.insert({a:6, Date1:new Date("01/08/2012")})
    

    first subquery
    db.test.distinct(‘a’, {…});

    [1, 2, 3]

    second subquery
    db.test.distinct(‘a’, {…});

    [ 1, 5, 6 ]

    (Date1 == null || Date1 <= today) && (Date2 == null || Date2 <= today)
    

    unwind

    Date1 == null && Date2 == null ||
    Date1 == null && Date2 <= today ||
    Date1 <= today && Date2 == null ||
    Date1 <= today && Date2 <= today ||
    

    query

    db.test.find(
    {
      $or : 
      [
        {$and: [
            {"Date1": {"$exists": false}},
            {"Date2": {"$exists": false}}
          ]},
        {$and: [
            {"Date1": {"$exists": false}},
            {"Date2": {
                "$exists": true,
                "$lte": new Date("2012-01-07T04:45:52.057Z")}
            }
          ]},
        {$and: [
            {"Date2": {"$exists": false}},
            {"Date1": {
                "$exists": true,
                "$lte": new Date("2012-01-07T04:45:52.057Z")}
            }
          ]},
        {$and: [
            {"Date2": {
                "$exists": true,
                "$lte": new Date("2012-01-07T04:45:52.057Z")}
            },
            {"Date1": {
                "$exists": true,
                "$lte": new Date("2012-01-07T04:45:52.057Z")}
            }
          ]}
      ]
    })
    >[ 1 ]
    

    this should work too (assume that ‘not exist’ and ‘null’ is the same)

    db.test.find(
    {
      $and : 
      [
        {$or: [
            {"Date1": null},
            {"Date1": { "$lte": new Date("2012-01-07T04:45:52.057Z")} }
          ]},
        {$or: [
            {"Date2": null},
            {"Date2": { "$lte": new Date("2012-01-07T04:45:52.057Z")} }
          ]}
      ]
    }
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to perform a SQL query that acts in two parts. First,
I am trying to perform a SQL query which generates a table with two
I'm having some problems trying to perform a query. I have two tables, one
I am trying to perform a query which groups a set of data by
I am trying to perform the following query Create a view which restricts staff
I have the following three tables which I am trying to perform a query
I'm trying to perform a linq to entities query on a table that's inherited
I am trying to perform a LINQ query on a DataTable and show the
I'm trying to perform a DISTINCT clause on a query like this SELECT DISTINCT
I am trying to achieve a query which includes a subquery which itself includes

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.