Neurelo Build Docs
Neurelo Build Docs
  • Introduction
    • Core Concepts
    • Key Features
  • Getting Started
    • Sign-in/Sign-up
    • Dashboard
      • Collapsible Sidebar
      • Light/Dark Mode
      • Account Settings
      • Audit Events
      • User Management
        • Permissions (Member v/s Admin)
      • Org Settings
    • Starting your Neurelo Project
      • Quick Start Guide
      • Step 1 - Add a Data Source
      • Step 2 - Build Definitions
      • Step 3 - Create an Environment
      • Step 4 - Create an API Key
      • Step 5 - Start Runners
      • Try your Neurelo APIs
  • "How to" Videos
    • Product Overview
    • Neurelo APIs & SDKs
    • Project Setup
    • Definitions
    • Environments
    • Data Sources
    • Organization Management
    • Creating and Using Custom Queries
    • Using the Schema Builder to build Relationships
    • Mock Data Generation
  • Definitions
    • Neurelo Schema Editor
      • Schema Builder
      • JSON/YAML Editor
      • Schema Visualization: Entity-Relationship Diagram (ERD)
    • Custom APIs for Complex Queries
      • Write and Commit Custom Queries
      • AI-Assisted Query Generation
      • Deploying Custom API Endpoints
      • Using Variables in your Custom Query
    • Branches and Commits
    • API Docs
  • Environments
    • Creating a new Environment
    • API Playground
    • Observability
    • Migrations
    • API Keys
  • Data Sources
    • PostgreSQL
    • MySQL
    • MongoDB
  • Guides
    • Provisioning Cloud Databases for using with Neurelo
      • PostgreSQL
        • AWS RDS (PostgreSQL)
      • MySQL
        • AWS RDS (MySQL)
      • MongoDB Atlas
    • Mock Data Generation
    • Wipe Data Source
    • Remote Git Repository for Definitions
      • Connecting a Remote Git Repo
      • Creating Commits from Neurelo
      • Syncing Branches
    • Data Viewer
    • Environment/Data Source Tags
    • How to work with Embedded documents and References in MongoDB
    • How to download and use the Postman Collection for your Project
    • Building Python applications with Postgres and FastAPI
    • CI Integration using Neurelo CLI
    • Schema Migrations
    • Schema AI Assist
    • Auto-Introspection
    • Access Policies
    • User Auth
      • Google
      • GitHub
      • GitLab
    • MongoDB Atlas - Migrate GraphQL to Neurelo
    • MongoDB Atlas - Migrate REST Data APIs to Neurelo
  • MongoDB Atlas - Migrate REST Data APIs to Neurelo
  • MongoDB Atlas - Migrate GraphQL APIs to Neurelo
  • Neurelo Schema Language (NSL)
    • Example 1 - DVD Rentals
    • Example 2 - Simple "Posts" App
    • Example 3 - Bookstore
  • Neurelo API Reference (REST)
    • Examples of Neurelo Auto-Generated REST API endpoints
      • Example 1 - Simple “Posts” application
      • Example 2 - "DVD Rentals" application
      • Example 3 - "Bookstore” application
      • cURL API Examples
  • Neurelo API Reference (GraphQL)
  • SDKs
    • TypeScript / JavaScript SDK
    • Go SDK
    • Python SDK
      • Python SDK Tutorial -- News Application
        • News Application using Neurelo’s Python SDKs
  • CLI (Preview Version)
  • Self-Hosted Neurelo Gateways
  • Tutorials
    • Building a Real Time Chat Application with Neurelo and MongoDB using Python
    • Building A Financial Terminal with Neurelo and MongoDB in Rust
    • Building a Restaurant Management System with Neurelo and MongoDB using GraphQL in just a few minutes
    • Bringing Neurelo’s Data APIs to Life Instantly with MySQL
  • Project Examples
  • References
    • Supported Databases
    • Supported OS and Browsers
  • Support
Powered by GitBook
On this page
  • Overview
  • Inner Objects (Embedded Documents)
  • References
  1. Guides

How to work with Embedded documents and References in MongoDB

PreviousEnvironment/Data Source TagsNextHow to download and use the Postman Collection for your Project

Last updated 11 months ago

Overview

MongoDB is a NoSQL database and does not support classic relationships as in SQL databases. However, you can build references across entities in MongoDB in a couple of ways -

  • Inner objects (equivalent to embedded documents, i.e., documents nested within another document)

  • References across collections using an ObjectID field in a collection to reference an ID in another collection

For a more comprehensive overview on this topic, refer to MongoDB's docs on . In this guide, we will see how you can work with inner objects and references in MongoDB with Neurelo.

Inner Objects (Embedded Documents)

An embedded document in MongoDB is a document nested inside another MongoDB document. That is, it is not just a reference to another document but it is fully contained within the parent document.

In the Neurelo Schema Language and in the APIs, embedded documents can be used as innerObjects.

For example, consider the following document

{
  "_id": "5fc89ecd1e602ff6c856e811",
  "name": "Twinkle Truffles",
  "flavor": "Milk Chocolate",
  "brand": "Cadbunny's",
  "price": 2.99,
  "inStock": true,
  "nutritionalInfo": {
    "calories": 250,
    "fat": "10g",
    "sugar": "25g",
    "protein": "5g"
  }
}

Here, nutritionalInfo is an inner object.

Inner objects enhance read performance by reducing database queries and ensuring atomic updates within a single document. They are optimal for closely related data that is frequently accessed together, remains relatively static, and adheres to the 16MB MongoDB document size limit. However, they might lead to data redundancy and scalability challenges as your database grows.

The Neurelo Schema for this will be something like this -

{
  "objects": {
    "foodItem": {
      "properties": {
        "id": {
          "type": "string",
          "identifier": true,
          "sourceName": "_id",
          "sourceType": "ObjectId",
          "default": {
            "function": "auto"
          }
        },
        "name": {
          "type": "string"
        },
        "flavor": {
          "type": "string"
        },
        "brand": {
          "type": "string"
        },
        "price": {
          "type": "number",
          "format": "float"
        },
        "inStock": {
          "type": "boolean"
        },
        "nutritionalInfo": {
          "$ref": "#/innerObjects/nutritionalInfo"
        }
      }
    }
  },
  "innerObjects": {
    "nutritionalInfo": {
      "properties": {
        "calories": {
          "type": "integer"
        },
        "fat": {
          "type": "string"
        },
        "sugar": {
          "type": "string"
        },
        "protein": {
          "type": "string"
        }
      }
    }
  }
}

References

References in MongoDB are used to link documents across different collections. This is a commonly used technique to represent one-to-one, one-to-many, or many-to-many relationships.

To do this, instead of embedding all data within a single document, we can reference data from other collections. For example, consider the following three documents from three different collections:

Here, there is a one to one reference between NutritionalInfo and Products. Furthermore, there is a one to many reference between Products and Reviews.

The Neurelo Schema for this will be something like this -

{
  "objects": {
     "nutritionalInfo": {
      "properties": {
        "id": {
          "type": "string",
          "identifier": true,
          "sourceName": "_id",
          "sourceType": "ObjectId",
          "default": {
            "function": "auto"
          }
        },
        "calories": {
          "type": "string"
        },
        "fat": {
          "type": "string"
        },
        "sugar": {
          "type": "string"
        },
        "protein": {
          "type": "string"
        }
      }
    },
    "products": {
      "properties": {
        "id": {
          "type": "string",
          "identifier": true,
          "sourceName": "_id",
          "sourceType": "ObjectId",
          "default": {
            "function": "auto"
          }
        },
        "name": {
          "type": "string"
        },
        "flavor": {
          "type": "string"
        },
        "brand": {
          "type": "string"
        },
        "price": {
          "type": "number",
          "format": "float"
        },
        "inStock": {
          "type": "boolean"
        },
        "nutritionalInfoId": {
          "type": "string",
          "sourceType": "ObjectId"
        },
        "reviewIds": {
          "type": "array",
          "items": {
            "type": "string",
            "sourceType": "ObjectId"
          }
        }
      }
    },
    "reviews": {
      "properties": {
        "id": {
          "type": "string",
          "identifier": true,
          "sourceName": "_id",
          "sourceType": "ObjectId",
          "default": {
            "function": "auto"
          }
        },
        "productId": {
          "type": "string",
          "sourceType": "ObjectId"
        },
        "rating": {
          "type": "integer"
        },
        "comment": {
          "type": "string"
        }
      }
    }
  }
}

Unlike embedded documents, references are more suitable for large or frequently changing data. They excel in many-to-many relationships by offering flexibility to link documents across different collections, preventing data duplication, and maintaining database normalization. However, this approach may require additional queries to access related data, potentially affecting performance.

Now, consider the same database structure as shown in the example above. To create references using Neurelo, it is important to structure your create queries in a specific order. This will ensure that all documents are created in the correct sequence to initiate the references between them. For instance,

  • You can begin by creating the NutritionalInfo document, as this does not depend on any other documents.

  • Next, you can create the Products document. Initially, you can include the nutritionalInfoId with the ID of the previously created NutritionalInfo document and initialize the reviewIds with an empty array.

  • After this, you can proceed with creating individual Reviews documents, where each document references the productId of the Products document.

  • And finally, you can update the Products document to include the _ids of the newly created Reviews documents.

For guidance on building APIs using inner objects, refer to the API reference materials for and .

For guidance on building create and update APIs, refer to the API reference materials for and .

Embedded Data Versus References
REST
GraphQL
REST
GraphQL