Rows vs Documents vs Sub-documents

Hello! I am new at MongoDB and I have imported this data set to play around with:

{
  "_id": {
    "$oid": "64459a3906135f434f43a1d5"
  },
  "data": [
    {
      "id": 34541863,
      "name": "\"A\" Cell Breeding Device",
      "type": "Spell Card",
      "frameType": "spell",
      "desc": "During each of your Standby Phases, put 1 A-Counter on 1 face-up monster your opponent controls.",
      "race": "Continuous",
      "archetype": "Alien",
    },
    {
      "id": 64163367,
      "name": "\"A\" Cell Incubator",
      "type": "Spell Card",
      "frameType": "spell",
      "desc": "Each time an A-Counter(s) is removed from play by a card effect, place 1 A-Counter on this card. When this card is destroyed, distribute the A-Counters on this card among face-up monsters.",
      "race": "Continuous",
      "archetype": "Alien",
{
      "id": 13026402,
      "name": "A-Team: Trap Disposal Unit",
      "type": "Effect Monster",
      "frameType": "effect",
      "desc": "This effect can be used during either player's turn. When your opponent activates a Trap Card, Tribute this face-up card to negate the activation of the Trap Card and destroy it.",
      "atk": 300,
      "def": 400,
      "level": 2,
      "race": "Machine",
      "attribute": "FIRE",
    }, ...

I’ve tried to do the equivalent of “Select * from DB where type = “Effect Monster”” on MongoDB Compass with this:

{“data.type”: {$eq: “Effect Monster”}}

This should retrieve only rows with type: “Effect Monster”

However I keep getting all entries and seems to be ignoring the “where” clause

Hello :wave: @Henry_Zheng,

Welcome to the MongoDB Community forums :sparkles:

The reason for this is that the query is retrieving documents where the type: "Effect Monster" matches and that contain more than one embedded sub-document. To obtain the desired result of the Effect Monster type, you must save each sub-document as a separate document.

For example:

Document 1:

{
  "_id": {
    "$oid": "64459a3906135f434f43a1d5"
  },
  "data": [
    {
      "id": 34541863,
      "name": "\"A\" Cell Breeding Device",
      "type": "Spell Card",
      "frameType": "spell",
      "desc": "During each of your Standby Phases, put 1 A-Counter on 1 face-up monster your opponent controls.",
      "race": "Continuous",
      "archetype": "Alien"
    }
  ]
}

and so on…

After that, if you query the data you will get the document with the specific type: "Effect Monster" :sparkles:


To clarify this further, let’s understand what documents and subdocument are in MongoDB:

  • A document is a group of data or information that is stored in a particular format, usually as a JSON or BSON object. For example, here are the 2 separate documents with specific _id. Please refer to ObjectId to learn more.

    {
      "_id": ObjectId("64459a3906135f434f43a1d5"),
      "data": [
        {
          "id": 34541863,
          "name": "Alex",
        }
      ]
    },
    
    {
      "_id": ObjectId("64459a3906135f434f43a1d5"),
      "data": [
        {
          "id": 34541864,
          "name": "Ben",
        }
      ]
    }
    
  • A row is a similar concept in the context of a relational database. Each row represents a single record or instance of data in the table, and each column represents a specific attribute or property of that record. For example:

    Customer ID Name
    34541863 Alex
    34541864 Ben
  • On the other hand, a subdocument is a document that is nested inside another document. It is essentially a field that contains another document as its value. For example:

    Here are the 2 subdocuments embedded under the same _id.

    {
      "_id": ObjectId("64459a3906135f434f43a1d5"),
      "data": [
        {
          "id": 34541863,
          "name": "Alex",
        },
        {
          "id": 34541864,
          "name": "Ben",
        }
      ]
    }
    

I suggest you refer to the following resources that should further help you further cement your understanding of these MongoDB concepts:

Hope this helps. Please let us know if there are any doubts about this.

Best,
Kushagra

2 Likes