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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:50:22+00:00 2026-06-07T17:50:22+00:00

i have a document: _id: ObjectId(THE DOCUMENT ID), GROUP NAME: First Floor, years: {

  • 0

i have a document:

"_id": ObjectId("THE DOCUMENT ID"),
   "GROUP NAME": "First Floor",
   "years": {
     "0": {
       "_id": "YEAR ID",
       "year_classes": {
         "0": {
           "class_name": "CLASS A",
           "_id": "CLASS ID",
           "participants": {
             "0": "PARTICIPANT ID" 
          } 
        },
         "1": {
           "class_name": "CLASS B",
           "_id": "CLASS ID",
           "participants": {
             "0": "PARTICIPANT ID" 
          } 
        },
      },
       "year_name": "YEAR 1" 
    },
     "1": {
       "_id": "YEAR ID",
       "year_classes": {
         "0": {
           "_id": "CLASS ID",
           "class_name": "CLASS C",
           "participants": [

          ] 
        } 
      },
       "year_name": "YEAR 2" 
    },
  } 

It is needed to add another class to a year. The code has the year id through the $YEAR_ID variable (is a string) which the class will be added to.

So, it has to check if the class name, given by $NEW_CLASS_NAME (string variable) has already been used in that year.

I saw the $and operator for mongo but i couldn’t manage to get it to work. If it were to be an SQL it would look likt: WHERE year_id=’$YEAR_ID’ AND class_name=’$NEW_CLASS_NAME’

How could this be done in MongoDB?


UPDATE:

The Query of:

{
“years.year_classes.class_name”: $NEW_CLASS_NAME,
“years._id”: $YEAR_ID
}

Is searching the document (so it will always return TRUE) not the same array.

Is there a way to direct the query to find those two values in the same array, so a class with that name, which is in the array of classes which is in the year array with that certain ID?

  • 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-07T17:50:24+00:00Added an answer on June 7, 2026 at 5:50 pm

    When you’re comparing two conditions inside an embedded object you need to use $elemMatch.
    See the general explanation here.

    Your query structure would be something like:

    db.coll.find({"years":{$elemMatch:{"year_classes.class_name":$NEW_CLASS_NAME,"_id":$YEAR_ID }}})

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

Sidebar

Related Questions

I have a document collection like this: { _id : ObjectId(4fb21439f31dfd122ce39c4a), Name : Freelander
I have this document stored in mongodb document: { _id:ObjectId(4eb7642ba899edcc31000001) hash:abcd123 value:some_text_here } I
I have the following document: {_id: '4eb79ee1e60fc603788e7259', Name: 'name', Subsidiaries: [ { _id: '4eb79eeae60fc603788e7271',
I have a document which includes a subdocument: { _id : ObjectId(XXXXX), SearchKey :
Using mongodb with the NoRM driver I have this document: { _id : ObjectId(0758030341b870c019591900),
I have one document in my params collection like this: { _id: ObjectId(4d124cef3ffcf6f410000037), code:
I have the following structure in my document: { _id : ObjectId(43jh4j343j4j), array :
I have a document which looks something like: { _id : ObjectId(4e84f78b26d2046d5d00b5b2), parent_id :
I have a countries document which looks like that: { _id : ObjectId(4e493af4140700590800154f), geoname_id
I have a document layout like this: Program = { '_id':ObjectId('4321...'), 'Title':'The Title', 'Episodes':[

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.