A MongoDB object-document mapper (ODM) library written in crystal which makes interacting with MongoDB or DocumentDB a breeze.
Uses the mongo.cr library under the hood that relies on the official MongoDB C Driver.
-
Add the dependency to your
shard.yml:dependencies: moongoon: github: elbywan/moongoon
-
Important: Install the official
MongoDB C Drivershared library (>= 1.15.1). -
Run
shards install
require "moongoon"
# A Model inherits from `Moongoon::Collection`
class User < Moongoon::Collection
collection "users"
index 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.removerequire "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:
Moongoon.connection { |db|
# "db" is a raw Mongo::Database instance.
# Check `mongo.cr` code for more details:
# https://github.com/elbywan/mongo.cr/blob/master/src/mongo/database.cr
# https://github.com/elbywan/mongo.cr/blob/master/src/mongo/collection.cr
cursor = db["my_collection"].find_indexes
while index = cursor.next
pp index
end
}require "moongoon"
class MyModel < Moongoon::Collection
collection "models"
# Define indexes
index 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# 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 `mongo.cr` driver shard syntax, but Models could
# be used for convenience despite a small performance overhead.
db["users"].update(
selector: {name: "John"},
update: {"$set": {"banned": true}},
flags: LibMongoC::UpdateFlags::MULTI_UPDATE
)
end
end- Fork it (https://github.com/your-github-user/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
- elbywan - creator and maintainer