State of mongodb drivers and integration with ecto

I currently have to deal with a mongodb database and I am quite confused about the state of the libraries when it comes to dealing with mongodb.

The one that I see being actively maintained is elixir-mongodb-driver by @zookzook , even though the actual hex package name is named mongodb_driver.

It seems that the library provides a few constructs that are ecto-alike with Mongo.Collection:

document do
    attribute :name, String.t(), default: "warning"
    attribute :color, String.t(), default: :red
  end

This is pretty nice, however it lacks the critical features of data validation provided by ecto and changesets. In the documentation it is specified that there is ecto support too: The Elixir Driver for MongoDB — mongodb-driver v1.5.0

The question, does this integration work as expected? It does seem like mongodb_ecto was updated for the latest driver version. Can it be used for production or I should just give up and use mongodb_driver only?

PS: I’ve also found library mongodb located in the same namespace as mongodb_ecto, seems like an alternative implementation of the driver?

It depends.

MongoDB offers numerous features that are not supported by Ecto. Some developers make the mistake of modelling MongoDB like a relational database. A certain amount of rethinking is necessary there. Basically, you do not need an Ecto layer, as you can formulate the statements directly with maps.

You can use the following:

  • plain MongoDB commands: just maps or keyword lists
  • plain MongoDB command + Mongo.Collection (structs instead of maps)
  • MongoEcto wrapper: data validation + changesets. For MongoDB specific features you have to switch back to plain MongoDB commands

The package mongodb_driver is no longer being developed. To be able to use the current MongoDB versions (6, 7, 8), I recommend the elixir-mongodb-driver :slight_smile:

3 Likes

Thanks for the answer!

Well, maybe the query layer is not very useful, however having ecto schemas and changesets is extremely valuable for data validation, but once again, this can be done with embedded schemas.

I cannot find the package elixir-mongodb-driver on hex, maybe I missed something?

Sorry for the confusion: You already found it: The Elixir Driver for MongoDB — mongodb-driver v1.5.0

2 Likes