Mongoid ODM
repository·master·Indexed 26 days ago
https://github.com/mongodb/mongoidAn 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.
What's inside Mongoid
- 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.
Run Mongoid tests
masterTo run the Mongoid test suite, you must first spin up a MongoDB deployment. The simplest method is to run a single MongoDB instance.
- Launch
mongodin one terminal:
mkdir /tmp/mdb mongod --dbpath /tmp/mdb- 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- Launch
Access Mongoid documentation and support
masterFor detailed usage and API information, refer to the following resources:
- User Documentation: https://www.mongodb.com/docs/mongoid/current/
- API Documentation: https://www.mongodb.com/docs/mongoid/current/api/
- Ruby Driver Documentation: https://www.mongodb.com/docs/ruby-driver/current/
Community Support:
- Stack Overflow: Use the
mongoidtag. - MongoDB Community Forum: Mongoid ODM section
Reporting Issues: Report bugs via the MONGOID project in the MongoDB JIRA.
Set up submodules for Mongoid tests
masterThe 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 updateNested attributes for many-to-n associations
masterWhen using
#accepts_nested_attributes_foron a many-to-n (many-to-many) association, Mongoid uses theMongoid::Association::Nested::Manybuilder 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:
- A
Hashof attributes. - An
Arrayof key/value pairs (e.g.,[[:key, 'value'], [:key2, 'value2']]). - An
Arraywhere the second element is a Hash (e.g.,[key, { hash_of_attrs }]).
If the attributes are not in one of these formats, an
ArgumentErroris 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
:limitoption is provided to the macro, the builder will raiseErrors::TooManyNestedAttributeRecordsif the number of attributes exceeds that limit.
- A
Configure polymorphic embeds_many associations
masterTo create a polymorphicembeds_manyassociation, use theasoption. Whenasis provided, Mongoid will automatically manage a type field to track the class of the embedded objects. The type field is named using the pattern#{as}_type.Configure polymorphic embeds_one associations
masterTo create a polymorphicembeds_oneassociation, use theasoption. Whenasis provided, Mongoid will automatically manage a type field to track the class of the embedded object. The type field is named using the pattern#{as}_type.Use polymorphic `has_one` associations
masterTo create a polymorphichas_oneassociation, use theasoption. Whenasis present, Mongoid treats the association as polymorphic and will automatically manage a type field to track the associated model type. The type field is named by appending_typeto the value provided in theasoption.Fix "Too many open files" error on MacOS
masterIf 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.
- Stop the MongoDB server.
- In the same terminal session where you will run the server, execute
ulimit -n 10000. - Restart the MongoDB server.
- Re-run the tests.
ulimit -n 10000Configure the storage field for embeds_many
masterBy default, Mongoid stores anembeds_manyassociation using the name of the association as the field key. You can override this behavior using thestore_asoption to specify a custom field name in the underlying BSON document.Check Mongoid compatibility
masterBefore using Mongoid, ensure your environment meets the following compatibility requirements:
- Ruby Implementations: MRI 2.7 - 4.0, JRuby 9.4 and 10.0
- MongoDB Server: 3.6 - 8.2
Use the embeds_many association
masterThe
embeds_manyassociation 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_manyassociation, 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_OPTIONSprovided by Mongoid.