How can we use MongoDb with postgresql?

When should I use MongoDB instead of PostgreSQL? (in web projects)

  • Nowdays, PostgreSQL has many stable extensions including hstore, arrays and JSON types for columns, so one can implement many document-oriented features in PostgreSQL. PostgreSQL is also stable and pretty fast. Some libraries support Postgres, but not Mongo (I'm a Rails developer). So, when is Mongo definitely a better choice for me?

  • Answer:

    Hey Based on my limited experience, I will happily say, for rapid development, that MongoDB has a large advantage. The primary reason, is that it's schemaless. With Postgres, it becomes rather tiresome to write migrations and maintaining them when starting out with a new design, especially if you are as vague as I am in exactly how the domain should be modelled. MongoDB gives you the luxury (and sin) of just throwing data at it and storing it down, where you can happily just add and remove attributes to your entities and have that persist all the way down to the DB. Why, even with an ORM like Mongoid, I can take any JSON formatted REST response, and create a searchable, strongly-typed and persisted document just be declaring an empty class that includes the Mongoid extension! No migrations needed. The sin, is that you can also remove fields and therefore 'lose' data! I'm sure this is where you will find TDD enthusiasts speaking up! The difference is that the whole document becomes your model, whilst in Postgres, the model is still your table, with the attributes of the model as serializable extensions. Given that, you ask what's the better choice. This stems from what you are trying to achieve. If you are looking for information critical, no-mistakes application development, than you would be foolish to ignore Relationship Integrity which Postgres provides. MongoDB advocates embedding any 'relational' data to the root aggregate (e.g. an address for a user is contained in the same User document, and not in two documents with a join. This could equate to a column in a User table in postgress of Address, with a hstore field type) Furthermore, when prototyping, you have more free storage and flexibility options with Postgres than Mongo. You may hit the limits quickly and find yourself spending money whilst still in prototype stages (this is coming from the viewpoint of developing your own personal project rather than being employed to do it!) In terms of Rails and MongoDB intertwining, I have had very few problems, and have found that I think more clearly about the solution knowing I have a more schema-free datastructure to comply too, which has led to perhaps not needing as many extensions. Also, another side effect or embedding documents, is that for administration purposes, you can readily get a sense of the entire model, without having to create views to join the data together. This sounds minor, but it actually is all part of the rapid development process. I guess in summary, my point of view is this. If you are looking to prototype an application, which is really why RoR got thrown into the limelight, MongoDB offers an even faster way to perform this. Whilst you lose the decades of research into relational databases, and it's stability, I think focussing on whether the idea works first, then given it's outcome, deciding how to make it robust, is a better option, with benefits in development time, and therefore economically too! If however, you are looking to create an application that cannot afford to 'lose' data, then perhaps stay with Postgres. I will say, you point mentions the extensions to Postgres. These do not afford the same semblance of stability as the underlying principles of RDBMS that Postgres does itself. So if you are looking to make good use of those, I think you may find more hurdles than if you use MongoDB. that's just a side thought though. Good luck

Sky Viker-Rumsey at Quora Visit the source

Was this solution helpful to you?

Other answers

There are a good interview on Russian with designer of MySQL. http://habrahabr.ru/company/jelastic/blog/166845/ We discussed this question some time ago.  As I remember, we concluded, that we should use PostgreSQL for strict structured or important data and no-SQL DBs (like Mongo) for available to be cached in RAM or not important or not often used data. In other way, we make next conclusion: SQL and noSQL DBs were created for different tasks and so differ by parameters (like performance, scalability and etc) on different data. This conclusion is not fully true, but I prefer it.

Maxim Borisyak

Most of the existing answers here completely miss the point of your question. PostgreSQL now has great native key/value and JSON support, so if you want to go "schemaless" like and leave modelling your domain until you already have a heap of data, you can just use an HStore/JSON column and route data straight to it. You can still use schemas on other tables without incurring the operational headache of using two different databases. High availability is not the same as write throughput, in fact they are somewhat opposed. Super-high "write" throughputs can be obtained in Mongo by configuring it to defer actually writing to disk until later. This makes it possible for you to lose data, the opposite of availability. PostgreSQL has no such option, so may be inappropriate if you don't want your data actually persisted. As of version 9.4, PostgreSQL benchmarks faster than MongoDB for both inserting and querying JSON data. See http://www.enterprisedb.com/postgres-plus-edb-blog/marc-linster/postgres-outperforms-mongodb-and-ushers-new-developer-reality (though no doubt both databases could be tuned for the specific benchmark). See also some good answers to and . In summary: probably never for any technical reasons.

Alex North

If you're unsure (and your choice is really limited to those two for some reason): use PostgreSQL. Reasons for using PostgreSQL: - PostgreSQL is ACID, which you probably want in 95% of your cases - PostgreSQL has nice non-relational storage support, such as JSON, JSONB, XML, etc. - PostgreSQL can be sharded (it might not be easy, but doable) - PostgreSQL has been around forever and is very mature - PostgreSQL also supports SQL and its many awesome language features Reasons for using MongoDB: - You know exactly what you're doing and why you're doing it, and it mostly has to do with distributing an eventual-consistent cache

Lukas Eder

I generally think that no-SQL databases solve specific hard dev-ops problems. i.e.  yahoo did not have a way to manage indexing every web page in the world so they created Hadoop to handle this eventually consistent unstructured data problem.   Now I am confused why people have spent so much time building infrastructure to stick structured data in this tool.  I rarely see unstructured data so I do not value Hadoop as much as others. Last time I checked High Availability I considered >= 300k writes per second a potential dev-ops problem for an RDBMS.   I have appreciated watching both Voldermort and Casandra manage this with little effort.  There are number of things that can get tricky for an RDBMS when dealing with data >= triple digit petabytes.   No-SQL has a few tricks to manage data at this scale as long as your questions are simple.   Though I think RDBMS with columnar storage are changing this a little.    It also seems strategic questions still need to end up in an RDBMS and the hybrid solution is to put a NoSQL in front to handle tactical near real time questions and let the RDBMS do more strategic questions. Graph modeling is not ideal in an RDBMS and gets messy really fast.  Neo4j is currently my favorite graph system and is Not Only SQL. Now if you do not know how or do not want to "waste" time with data modeling many NoSQL solutions offer you a way to treat all data the same in some kind of  "document" of key value stores.  I have easily done this in a mature RDBMS as well but for those that consider modeling a waste of time it is unlikely they will know how to do it.  Many people will say key value stores belong exclusively in NoSql .   I personally think NoSQL is a fine option for key value stores but not always ideal.  I am in general fond of hybrid solutions and I have no problem with using RDBMS to do Key Value Stores.  Especially because I can combine other data models with the Key Value Store and make a much more versatile app without needing to manage multiple  platforms or overloading key value stores in ways they were not designed for.  I think people forget that a key value store model that self join is still a relation of data.  Managing relations of data is what a mature RDBMS does great.   Now not all RDBMS platforms handle recursive models well but both Postgres and Oracle easily do.  Oracle having by far the best functions for recursively traversing a key value store in a lot of interesting ways.   This is not even going into other often important things to consider like data encryption, row level security, compression, logging, governance, instrumentation, admin which all will require extra work in NoSQL land to do very much more than rudimentary things.  I can say in a RDBMS I have built a key value store up to a billion of rows with sub second response with no problems.   Now databases are not hard for me to setup or do development in. I have been in some fortune 500 companies where that is not the normal experience for everyone.  For these companies and even smaller ones with similar problems getting rid of DBA's and DevOps is a win.  In places like this it is less about the right tech choice and more about NoSQL := NoDBA xor NoDevOPS.  NoSQL can solve staffing problems which is probably its second biggest virtue.

Andrew Hansen

Comparing MongoDB 3 to PostgreSQL 9.4, MongoDB is good if your application is: Storing JSON documents (and only JSON documents) with a large (20+) number of fields, and, Doing ad-hoc queries (no prior indexes) on 1-3 of those fields, and, For some reason, it's impossible for you to parse the data into relational data, and, You don't need any of the other features of an RDBMS. or, You need the automatic sharding features of MongoDB (and you actually do need them, you don't just think that you do), and you fully understand the limitations and systems administration load better Otherwise, PostgreSQL will meet your needs better. PostgreSQL's JSON storage features are as fast or faster than MongoDB 3's for most typical JSON document sizes, and it has so many other features as to make the comparison rather unfair. You can find a talk I just gave, with a brief comparison of MongoDB 3 and PostgreSQL 9.4, here: http://thebuild.com/blog/2015/03/28/postgresql-and-json-2015/

Christophe Pettus

There's a tremendous amount of momentum behind relational databases since they've been with us since the 70s so keep this in mind when you read opinions on this subject. There are a lot of companies and careers built around this technology and you should not expect unbiased answers. Personally, I had used (and loved) relational databases for about 22 years before using mongoDB. My opinion is that the best reason to use MongoDB is that it allows data to remain in its most natural format. If you've done any programming in an object-oriented language with a relational database, you know the pain of mapping tables to objects. Not suggesting ripping out all relational databases tomorrow, but also suggesting that everyone take a harder look at noSQL databases such as MongoDB before reaching a decision. I've written a blog post that may be of interest that describes our experience of migrating from SQL to MongoDB. https://www.watercooleranalytics.com/blog/561eb31abcdc214560000001

Mark Clancy

If you use a lot of normalized data, go postgres In most of the other case, go mongoDb MongoDb offers a very flexible way to store object, and can be easily mapped to your application object. So you can forgot the n to n relations tables which were a complete mess. For instance, storing a Tweet in mongodb (and I know what I am talking about because I store millions of them in mongoDb AND postgresql every hour) is a single line in MongoDB Also, mongoD offers sharding so it can be easily scaled on a cluster, which is much harder to hachieve with Postgres On the other hand, no doubt that Postgres is faster especially for aggreation type of operation. But what I really like with MongoDb is that you get rid of the "SQL way of thinking", with schema, migration, and so on. This opens a lot of possibilities

Landspurg Thomas

MongoDB is great for quick prototyping and it integrates easily with the node.js stack. And honestly that's about it, there really are that much advantages over PostgreSQL.

Vilis

Rajesh Raveendran

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.