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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:17:47+00:00 2026-06-06T02:17:47+00:00

I am using PHP/Mongo to query a data set like the following: { description:

  • 0

I am using PHP/Mongo to query a data set like the following:

{
  description:
  {
    0:
      {level: (some int 0-50)},
    1:
      {level: (some int 0-50)},

  ...

    n:
      {level: (some int 0-50)}
   }
 }

How do I write the function, in php, that will build a dynamic query that searches for a level 40 and above, using $or and based on the number of description keys (0 … n)?

What I currently have for a manual query is not working (I have a feeling it’s because in some entries description.1, 2, 3 do not exist):

find("$or":{"description.0.level":[40,41,42,43,44,45,46,47,48,49,50],"descriptio‌​n.1.level":[40,41,42,43,44,45,46,47,48,49,50],"description.2.level":[40,41,42,43,‌​44,45,46,47,48,49,50],"description.3.level":[40,41,42,43,44,45,46,47,48,49,50]}})

It gives me an error:

Fatal error: Uncaught exception ‘MongoCursorException’ with message ‘$and/$or/$nor must be a nonempty array’

So, I need a function that will build the query dynamically, and work :).

  • 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-06T02:17:48+00:00Added an answer on June 6, 2026 at 2:17 am

    For those interested, here is my final code for this problem (works).

    $y = 0;
    $descriptionLevels = 10; // set to 'n' levels as needed ...
    while($y < $descriptionLevels){
        $filter['$or'][]['description.' . $y . '.level']['$in'] = range($startLevel, $endLevel);
        $y++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using PHP to talk with a Mongo use findOne($query) on a db.collection it
I'm trying to update a mongo document, using PHP and the update() function. However,
Using PHP, is there a function/method/way to check if a variable contains something that
Using PHP, I can convert MySQL data or static table data to csv, Excel,
Using Php I would like to extract the current Url of a page including
I'm using the nginx/PHP/MongoDB stack and trying to set up my development environment on
I need to update multiple embedded docs in mongo using PHP. My layout looks
The PHP documentation for the mongo class says using a cursor instead of iterator_to_array
Using PHP, Perl, or Python (preferably PHP), I need a way to query an
Using PHP, what's the fastest way to convert a string like this: 123 to

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.