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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:33:08+00:00 2026-06-06T16:33:08+00:00

I am using the MongoDB PHP Library and have the following query array Array

  • 0

I am using the MongoDB PHP Library and have the following query array

Array
(
  [$and] => Array
    (
      [0] => Array
        (
          [$or] => Array
            (
              [0] => Array
                (
                  [role.name] => Array
                    (
                      [$in] => Array
                        (
                          [0] => 'User'
                          [1] => 'VIP User'
                        )
                    )
                )
              [1] => Array
                (
                  [role._id] => Array
                    (
                      [$in] => Array
                        (
                          [0] => 'User'
                          [1] => 'VIP User'
                        )
                    )
                )
            )
        )
    )
)

Which does not work. However, this does:

Array
(
  [$and] => Array
    (
      [0] => Array
        (
          [role.name] => Array
            (
              [$in] => Array
                (
                  [0] => 'User'
                  [1] => 'VIP User'
                )
            )
        )
    )
)

Any idea what the problem is? (This is an abstracted example)

  • 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-06T16:33:10+00:00Added an answer on June 6, 2026 at 4:33 pm

    I don’t see a problem with your queries, apart from the redundant $and operator. Consider the following Mongo shell example:

    > db.foo.drop()
    true
    > db.foo.insert({role:{name:"User"}});
    > db.foo.insert({role:{name:"Admin"}});
    > db.foo.insert({role:{name:"VIP User"}});
    > db.foo.find({$or:[{"role.name":{$in:["User", "VIP User"]}}, {"role._id":{$in:["User", "VIP User"]}}]});
    { "_id" : ObjectId("4fea16dcdb49621e28c004a2"), "role" : { "name" : "User" } }
    { "_id" : ObjectId("4fea16e8db49621e28c004a4"), "role" : { "name" : "VIP User" } }
    > db.foo.find({$and:[{$or:[{"role.name":{$in:["User", "VIP User"]}}, {"role._id":{$in:["User", "VIP User"]}}]}]});
    { "_id" : ObjectId("4fea16dcdb49621e28c004a2"), "role" : { "name" : "User" } }
    { "_id" : ObjectId("4fea16e8db49621e28c004a4"), "role" : { "name" : "VIP User" } }
    

    Note that there isn’t a role._id field in my data, so it was simply ignored.

    If you’re having trouble with the JavaScript shell, it may be helpful to walk through the examples in the $or and $and documentation. Both operators expect an array as their value, which in turn can contain any number of valid criteria objects.

    The main benefit of $and is when you need to satisfy multiple criterion for the same field. If the field names are different, it’s equivalent to simply using a criteria object (where keys are implicitly and-ed).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using PHP Yii Framework with MongoDB(yiimongodbsuite). I have created a Model which
So I'm building an app using PHP and MongoDB which will have a fair
how to insert inner array in mongodb using php I run this command in
I would love to sort an embedded MongoDB object using PHP Lithium. I have
I'm using MongoDB with PHP to store songs which users add via a basic
I have inserted and fetched data using MongoDB, in PHP. Is there an actual
I have the following mongodb object: { _id: ObjectId(4d0b9c7a8b012fe287547157), messages: { 0: { toUname:
I'm using php sesions with mongodb by defining the session_set_save_handler() if every user has
I'm using MongoDB with PHP's own driver and I have saved multiple arrays under
In MongoDB-PHP I am using the following example code to push a new entry

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.