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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:55:38+00:00 2026-06-05T18:55:38+00:00

I am trying to search a MongoDB database filtering on a specific data item.

  • 0

I am trying to search a MongoDB database filtering on a specific data item. Specifically, I want USA = PG-13 for my filter.

How to make a request: to get movies, where MPAA for USA = PG-13?

collection country:

{
   "_id": ObjectId("4fd79ec34c9fda9d05000080"),
   "en": "USA" 
}
{
   "_id": ObjectId("4fd79ec34c9fda9d0500007f"),
   "en": "Hong Kong" 
}

collection movie:

{
   "_id": ObjectId("4fd79ec34c9fda9d05000081"),
   "movieId": {
     "imdb": "0848228" 
  },
   "movieTitle": "The Avengers",
   "movieYearSpan": {
     "start": "2012",
     "end": "2012" 
  },
   "movieType": "Movie",
   "movieMpaa": {
     "verdict": "Rated PG-13",
     "0": {
       "mpaa": "IIA",
       "country": ObjectId("4fd79ec34c9fda9d0500007f") 
    },
     "1": {
       "mpaa": "PG-13",
       "country": ObjectId("4fd79ec34c9fda9d05000080") 
    } 
  } 
}

I tried to first get the ID for the USA.

$cursorCountry = $collectionCountry->find(array("en" => "USA"));
$idCountry = $cursorCountry->getNext();
$_id = $idCountry["_id"];
$cursorMovie = $collectionMovie->find(array("movieMpaa.country" => $_id, "movieMpaa.mpaa" => "PG-13"));

Does not work!
How then to make a request?
To get movies, where MPAA for USA = PG-13?

  • 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-05T18:55:40+00:00Added an answer on June 5, 2026 at 6:55 pm

    As your data is laid out, there is no good query here.

    If you can change your movies collection to the following, then your query will work:

       "movieMpaa": {
         "verdict": "Rated PG-13",
         "countries": [
             {
               "mpaa": "IIA",
               "country": ObjectId("4fd79ec34c9fda9d0500007f") 
           },
             {
               "mpaa": "PG-13",
               "country": ObjectId("4fd79ec34c9fda9d05000080") 
            } 
         ]
    

    When movieMpaa.countries is an array of objects, then you can query into that array for movieMpaa.countries.country. MongoDB will recognize the array and “drill into” the objects.

    However, there is another way structure this that probably much easier longer term:

       "movieMpaa": {
         "verdict": "Rated PG-13",
         "countries": {
             ObjectId("4fd79ec34c9fda9d0500007f") : { "mpaa": "IIA" },
             ObjectId("4fd79ec34c9fda9d05000080") : { "mpaa": "PG-13" }
         }
    

    If a country can only have one rating, then technically that countries value is a dictionary of Countries => ratings. The structure above stores them this way.

    However, the use of ObjectId is a little ugly there. Note that you can override the ID in the country collection. Consider using the 2 or 3-character ISO codes instead. These are easier to read.

       "movieMpaa": {
         "verdict": "Rated PG-13",
         "countries": {
             "UK" : { "mpaa": "IIA" },
             "US" : { "mpaa": "PG-13" }
         }
    
    db.movies.find({'movieMpaa.countries.US': 'PG-13'})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to search a document for data on a specific column. I am
Im trying to search two of the fields in my table. (Database table contains
I'm trying to search for data in a XML File using php. I'm not
I want to regex search an integer value in MongoDB. Is this possible? I'm
I trying to search a MySQL database using PHP, The search works fine but
I am trying to search all image tags on a specific page. An example
I am trying to search within a string for a specific value. What I
I am trying to search for the substring abc in a specific file in
I'm using MongoDB with Mongoid and trying to put in place a rudimentary search
I am trying to display a search rank along with the results using MongoDB

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.