Mongoid ODM

repository·master·Indexed 26 days ago

https://github.com/mongodb/mongoid

An Object-Document Mapper (ODM) for Ruby that allows developers to interact with MongoDB using Ruby objects instead of raw BSON/JSON documents. Built on top of the official MongoDB Ruby driver, it supports Ruby implementations MRI 2.7 - 4.0 and JRuby 9.4 and 10.0, and MongoDB Server versions 3.6 - 8.2.

Tokens
4.6K
Snippets
8
Records
38
Agent score
84%

What's inside Mongoid

  1. Overview of Mongoid ODM

    master
    Mongoid is an Object-Document Mapper (ODM) framework for MongoDB in Ruby. It provides a high-level interface for mapping Ruby objects to MongoDB documents, built on top of the official MongoDB Ruby driver.
  2. Run Mongoid tests

    master

    To run the Mongoid test suite, you must first spin up a MongoDB deployment. The simplest method is to run a single MongoDB instance.

    1. Launch mongod in one terminal:
    mkdir /tmp/mdb
    mongod --dbpath /tmp/mdb
    1. Run the test suite in a separate terminal using rake:
    rake
    # Launch mongod in one terminal
    mkdir /tmp/mdb
    mongod --dbpath /tmp/mdb
    
    # Run the test suite in a separate terminal
    rake
  3. Access Mongoid documentation and support

    master

    For detailed usage and API information, refer to the following resources:

    Community Support:

    Reporting Issues: Report bugs via the MONGOID project in the MongoDB JIRA.

  4. Set up submodules for Mongoid tests

    master

    The Mongoid test suite relies on shared tooling stored in a separate repository via git submodules. To ensure the test environment is complete, you must initialize and update the submodules after checking out the desired branch.

    git submodule init
    git submodule update
  5. Nested attributes for many-to-n associations

    master

    When using #accepts_nested_attributes_for on a many-to-n (many-to-many) association, Mongoid uses the Mongoid::Association::Nested::Many builder to process attribute assignments. This builder performs one of three operations for each set of attributes: updating an existing document, replacing an existing document with a new one, or removing a document.

    Attribute Format Requirements Attributes must be provided as either:

    1. A Hash of attributes.
    2. An Array of key/value pairs (e.g., [[:key, 'value'], [:key2, 'value2']]).
    3. An Array where the second element is a Hash (e.g., [key, { hash_of_attrs }]).

    If the attributes are not in one of these formats, an ArgumentError is raised with the message: "Attributes for nested association '<association_name>' must be a Hash or an Array of key/value pairs."

    Constraints

    • Limit: If a :limit option is provided to the macro, the builder will raise Errors::TooManyNestedAttributeRecords if the number of attributes exceeds that limit.
  6. Fix "Too many open files" error on MacOS

    master

    If you encounter a "Too many open files" error on the MongoDB server while running tests on MacOS, you need to increase the file descriptor limit.

    1. Stop the MongoDB server.
    2. In the same terminal session where you will run the server, execute ulimit -n 10000.
    3. Restart the MongoDB server.
    4. Re-run the tests.
    ulimit -n 10000
  7. Use the embeds_many association

    master

    The embeds_many association is used to embed multiple documents within a single parent document. This creates a one-to-many relationship where the child documents are stored directly inside the parent's BSON structure rather than in a separate collection.

    Available Options

    When defining an embeds_many association, you can use the following specific options:

    • as: Defines a polymorphic association.
    • cascade_callbacks: Determines if callbacks on the parent should cascade to the embedded documents.
    • cyclic: Enables cyclic dependencies.
    • order: Specifies the field used for ordering the embedded documents.
    • store_as: Specifies the field name used to store the list of embedded objects in the database (defaults to the association name).
    • before_add: Callback triggered before an object is added to the association.
    • after_add: Callback triggered after an object is added to the association.
    • before_remove: Callback triggered before an object is removed from the association.
    • after_remove: Callback triggered after an object is removed from the association

    Note: These are in addition to the SHARED_OPTIONS provided by Mongoid.