MongoDB with Phoenix & Ecto

Hey all,

I’m looking into how to use MongoDB with Phoenix.

My first thought is to use the Mongo.Ecto adapter. However, it looks like Mongo.Ecto is incompatible with Ecto 1.1 (phoenix_ecto 2.0 uses ecto 1.1.9 out of the box).

It also looks like the work to support ecto 2.0 is still a work in progress.

Does this mean that my best bet for using MongoDB within Phoenix (right now) is to use ericmj’s raw Mongo driver, and bypass Ecto completely? I’m fine with this if that’s the case - mongodb looks like a solid package and has been working great in my experiments. I’m just wondering if I’m missing something.

Thanks!

i also wanted to know the same thing .

I don’t have an alternative for you other than the one you suggested but I couldn’t live with myself if I didn’t ask you to seriously consider if MongoDB is what you really want…

http://cryto.net/~joepie91/blog/2015/07/19/why-you-should-never-ever-ever-use-mongodb/

PERHAPS some of these problems have been fixed since 2015 but the glaring security oversights and failings in basic database design make me question anything they come out with in the future.

P.S. / EDIT: Not for a minute suggesting you didn’t consider your requirements and not trying to start a flame-war - if anything I am just hoping to draw attention to some issues you might not have seen and if it convinces you not to use MongoDB technically it’s a solution to your above issue :stuck_out_tongue:

3 Likes

I concur. The only reason you would like to use MongoDB (in my humble opinion) is that your app is already using it. And even then, you probably would be better off migrating the data to Postgres. My team wrote a blog post about how they did a migratiion from MongoDB to Postgres about a year ago. It’s written in context of Rails app, but you can adapt to Elixir, or migrate your existing data this way.

2 Likes

Thanks guys,

Unfortunately, I’m not in a position to be asking if we should be using MongoDB. Many projects I’m working on are fairly intimately tied to it at this point.

I’m just looking for the best way to use MongoDB in an Elixir context. Looks like I’ll be going with the mongodb package and bypassing Ecto.

Hey all,

I ended up writing an article that summarizes everything I’ve learned. The moral of the story is that ericmj’s MongoDB driver is currently your best bet for integrating with Mongo.

I hope this helps some people out there!

5 Likes

Good article, a few notes. :slight_smile:

Mongo.Ecto is currently incompatible with Ecto 1.1, which is the version of Ecto currently used out of the box by the Phoenix framework.

Ecto 2.0 is currently what is used out of the box with Phoenix, not Ecto 1.1. :slight_smile:

And if your Final Thoughts, maybe hint to people to make PR’s to the Mongo.Ecto project to get it updated? ^.^

/me knows that some people poorly made a decision to bind themselves to mongo long ago, but no one, ever, should be using mongo for a new project nowadays.

1 Like

Really? In a Phoenix project I’m working on I see that I’m pulling in phoenix_ecto 2.0. In my mix.lock I see that while I’m using phoenix_ecto 2.0.2, I’m using ecto 1.1.0. Based on that I assumed that Phoenix was pulling in the 1.1 version of Ecto:

%{
  ...
  "ecto": {:hex, :ecto, "1.1.9"},
  ...
  "phoenix": {:hex, :phoenix, "1.1.6"},
  "phoenix_ecto": {:hex, :phoenix_ecto, "2.0.2"},
  ...
}

Ah that is because you are using Phoenix 1.1, which is a fairly old version now. Phoenix 1.2 has been the release version for a while now and 1.3 is about to be released too. :slight_smile:

Doh… Thanks!

Using phoenix With MongoDb

Hi guys, this article looks helpful for those who want to start Phoenix With Mongodb.

3 Likes

In my case, I started looking into this article to understand how I might use CouchDB with Phoenix and Ecto. So I recommend it!

2 Likes

Just a link to put something on the other side of the “don’t use Mongo” scale

https://blog.serverdensity.com/does-everyone-hate-mongodb/

to be fair this is pretty old and does raise the issues and bugs that have been long fixed in MongoDB.

it seems mongo adapter now support ecto2.0

1 Like

I’ve barely played with MongoDB so I am wondering why someone would choose it over, say, PostgreSQL?

At the moment there is no reason. But remember that MongoDB started when we were still running Potgres 8.x so many featurea like JSON support were not in it.

1 Like

I like the document based mindset. I’m not a database expect by any measure, but it seems like most of the Elixr community is blindly in love with Postgres(?)

I would chose Mongo just because it’s designed as a document based database instead of it being a relational database trying to be everything for everybody.

By the same irrational thinking I would test out CouchDB. It’s written in Erlang, after all

1 Like

That is because PostgreSQL’s jsonb datatype can be a json document storage, however you can still query deep in it, you can build indexes on structure or data or both, and much more. It is very very powerful and very very fast. :slight_smile:

1 Like

It seems so :slight_smile:

I use MongoDB in production since 2012. I think, you are right: many people try to use the relational database concept for everything. Even if they try to use the MongoDB. Then they get disappointed and talk about their sad experiences (greatly generalize their experiences). In my opinion one should look at the different databases and try them out. It is still better than to report permanently on bad experiences. In some use cases a text file is the best database you can use. The text search lucene framework (from the Java world) can act like a database. It is possible to combine to different databases to speed up things. Use the lucene framework (for Example with the elasticsearch server) for searching and the MongoDB/CouchDB for fetching the data from the result set. Don’t waste work and use a cache to store the objects for some time. And now your solution is very fast.