moongoon
Version, currently 0.7.423 versions
- 0.7.4latestMay 4, 2021
- 0.7.3not indexedOct 11, 2024
- 0.7.2not indexedOct 11, 2024
- 0.7.1not indexedOct 11, 2024
- 0.7.0not indexedOct 11, 2024
- 0.6.0not indexedOct 11, 2024
- 0.5.1not indexedOct 11, 2024
- 0.5.0not indexedOct 11, 2024
- 0.4.1not indexedOct 11, 2024
- 0.4.0not indexedOct 11, 2024
- 0.3.1not indexedOct 11, 2024
- 0.3.0not indexedOct 11, 2024
- 0.2.9not indexedOct 11, 2024
- 0.2.8not indexedOct 11, 2024
- 0.2.7not indexedOct 11, 2024
- 0.2.6not indexedOct 11, 2024
- 0.2.5not indexedOct 11, 2024
- 0.2.4not indexedOct 11, 2024
- 0.2.3not indexedOct 11, 2024
- 0.2.2not indexedOct 11, 2024
- 0.2.1not indexedOct 11, 2024
- 0.2.0not indexedOct 11, 2024
- 0.1.0not indexedOct 11, 2024
github.com/elbywan/moongoon
An object-document mapper (ODM) for MongoDB.
47 stars
1 dependent
License: MIT
Installation
# Add this to your shard.yml
dependencies:
moongoon:
github: elbywan/moongoon
version: ~> 0.7.4Then run:
shards installshard.yml
- Crystal
>= 0.35.0, < 2.0.0- License
- MIT
- Author
- elbywan
- Target
moongoonfrom src/moongoon.cr
Dependencies
Runtime Dependencies
- cryomongo~> 0.3.0github: elbywan/cryomongo
README
An object-document mapper (ODM) library written in Crystal which makes interacting with MongoDB a breeze.
This library relies on:
For the moongoon version relying on the mongo.cr driver, please check the mongo.cr branch.
Installation
- Add the dependency to your
shard.yml:
dependencies:
moongoon:
github: elbywan/moongoon
-
Run
shards install -
Profit! đź’°
Usage
Minimal working example
require "moongoon"
# A Model inherits from `Moongoon::Collection`
class User < Moongoon::Collection
collection "users"
index keys: { name: 1, age: 1 }, options: { unique: true }
property name : String
property age : Int32
property pets : Array(Pet)
# Nested models inherit from `Moongoon::Document`
class Pet < Moongoon::Document
property pet_name : String
end
end
# Connect to the mongodb instance.
Moongoon.connect("mongodb://localhost:27017", database_name: "my_database")
# Initialize a model from arguments…
user = User.new(name: "Eric", age: 10, pets: [
User::Pet.new(pet_name: "Mr. Kitty"),
User::Pet.new(pet_name: "Fluffy")
])
# …or JSON data…
user = User.from_json(%(
"name": "Eric",
"age": 10,
"pets": [
{ "pet_name": "Mr. Kitty" },
{ "pet_name": "Fluffy" }
]
))
# …or from querying the database.
user = User.find_one!({ name: "Eric" })
# Insert a model in the database.
user.insert
# Modify it.
user.name = "Kyle"
user.update
# Delete it.
user.remove
Connecting
require "moongoon"
Moongoon.before_connect {
puts "Connecting…"
}
Moongoon.after_connect {
puts "Connected!"
}
# … #
Moongoon.connect(
database_url: "mongodb://address:27017",
database_name: "my_database"
)
# In case you need to perform a low level query, use `Moongoon.client` or `Moongoon.database`.
# Here, *db* is a `cryomongo` Mongo::Database. (For more details, check the `cryomongo` documentation)
db = Moongoon.database
cursor = db["my_collection"].list_indexes
puts cursor.to_a.to_json
Models
require "moongoon"
class MyModel < Moongoon::Collection
collection "models"
# Note: the database can be changed - if different from the default one
# database "database_name"
# Define indexes
index keys: { name: 1 }
# Specify agregation pipeline stages that will automatically be used for queries.
aggregation_pipeline(
{
"$addFields": {
count: {
"$size": "$array"
}
}
},
{
"$project": {
array: 0
}
}
)
# Collection fields
property name : String
property count : Int32?
property array : Array(Int32)? = [1, 2, 3]
end
# …assuming moongoon is connected… #
MyModel.clear
model = MyModel.new(
name: "hello"
).insert
model_id = model.id!
puts MyModel.find_by_id(model_id).to_json
# => "{\"_id\":\"5ea052ce85ed2a2e1d0c87a2\",\"name\":\"hello\",\"count\":3}"
model.name = "good night"
model.update
puts MyModel.find_by_id(model_id).to_json
# => "{\"_id\":\"5ea052ce85ed2a2e1d0c87a2\",\"name\":\"good night\",\"count\":3}"
model.remove
puts MyModel.count
# => 0
Running scripts
# A script must inherit from `Moongoon::Database::Scripts::Base`
# Requiring the script before connecting to the database should be all it takes to register it.
#
# Scripts are then processed automatically.
class Moongoon::Database::Scripts::Test < Moongoon::Database::Scripts::Base
# Scripts run in ascending order.
# Default order if not specified is 1.
order Time.utc(2020, 3, 11).to_unix
def process(db : Mongo::Database)
# Dummy code that will add a ban flag for users that are called 'John'.
# This code uses the `cryomongo` syntax, but Models could
# be used for convenience despite a small performance overhead.
db["users"].update_many(
filter: {name: "John"},
update: {"$set": {"banned": true}}
)
end
end
Contributing
- Fork it (https://github.com/elbywan/moongoon/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
See the contributors page.
Credit
- Icon made by Smashicons from www.flaticon.com.
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.7.4- Tagged
- May 4, 2021
- Commit
81de8b070c2e- Crystal
>= 0.35.0, < 2.0.0- Indexed
- yes
Dependents
Repository
github.com/elbywan/moongoon
Metadata
- Created
- Aug 12, 2026
- Updated
- Sep 24, 2026
- Synced
- Sep 23, 2026
- Versions
- 23