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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:16:56+00:00 2026-05-15T07:16:56+00:00

in mysql i use JOIN and one query is no problem. what about mongo?

  • 0

in mysql i use JOIN and one query is no problem. what about mongo?

imagine categories and products.

products may have more categories.
categories may have more product.
(many to many structure)
and administrator may edit categories in administration (categories must be separated)

its possible write product with categories names in one query?

i used this structure

categories {
   name:"categoryName",
         product_id:["4b5783300334000000000aa9","5783300334000000000aa943","6c6793300334001000000006"]
}


products  {
    name:"productName",
category_id:["4b5783300334000000000bb9","5783300334000000000bb943","6c6793300334001000000116"]
}

now i can simply get all product categories, and product in some category and categories alone for editation. but if i want write product with categories names i need two queries – one to get product categories id and second to get categories names from categories by that ids.

is this the right way? or this structure is unsuitable? i would like to have only one query but i dont know if its possible.

  • 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-15T07:16:57+00:00Added an answer on May 15, 2026 at 7:16 am

    Yep, MongoDB is specifically bad at this particular type of operation. However, it’s also a matter of scope. If you have 30 million products and you want to join the 3 million Products to their Category, you’ll find that it’s not very quick in many Databases (even though it’s one line of code).

    What MongoDB requires here is some de-normalization.

    Your collections will probably look like this:

    categories {
      _id:"mongoid",
      name:"categoryName",
      ...
    }
    
    products  {
      _id:"mongoid",
      name:"productName",
      categories:[
        {"4b5783300334000000000bb9":'categoryName'},
        {"5783300334000000000bb943":'CategoryName2'},
        {"6c6793300334001000000116":'categoryName3'}
      ]
    }
    

    Here’s how it will work (rough estimate, don’t have my Mongo instance handy):

    Grab products and their categories

    db.products.find({"_id": {$in:[1,2,3]}, {name:1,categories:1})

    Grab products with a certain category:

    db.products.find({"categories.0": {$in:[x,y,z]}},{categories:1,name:1} }

    Yes, it’s not quite the same. And you will have to will have to update all of the product records when you change the name of a category. I know this seems weird, but this is standard for denormalization.

    BTW, I’m assuming here that you’re going to have lots of products and significantly less categories. This method is probably best if you have say 100 items in each category. If you have 5 items in each category, then you’ll probably want to store more of the category information in each item.

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

Sidebar

Related Questions

I have a MySQL query that I use to retrieve random rows from a
I'm about to use MySQL with Hibernate on a Tomcat 5.5.x server. Do I
I have an ASP.net project I'm looking at and they want to use MySQL.
One of my projects use the MyISAM engine in MySQL, but I'm considering changing
I use MySQL in a fairly complex web site (PHP driven). Ideally, there would
What database should a beginner use in the C language? Can I use MySQL?
We are porting an app which formerly used Openbase 7 to now use MySQL
Can MySql 5.0 views use tables that are located on another server? What is
I used to use the standard mysql_connect(), mysql_query(), etc statements for doing MySQL stuff
How do you build and use dynamic sql in a MySQL stored procedure?

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.