Also I still have a question to those that actually use MongoDB in real world applications.
Is it normal to nest objects?
So lets say I have document with blog entries with following structure:
- _id
- title
- text
- date
- comments[]
Then comments have following structure:
- author ObjectID(_id)
- date
- comment
Well nothing big, but what if I have 1000+ comments? Searching would be pain in the butt and I don't know about loading times.
In that case would it be more efficient to store objectIds in comments as an array and then get those comments from different document based on object id? So following structure:
Blog Entries:
- _id
- title
- text
- date
- comments[ObjectID(...), ObjectID(...), ObjectID(...), ObjectID(...) ...]
Comments:
- _id
- author ObjectID(_id)
- date
- comment
Users:
- _id
- username
- password
- email
---------- Post added at 01:13 PM ---------- Previous post was at 12:22 PM ----------
Also another question. Let's say I have setup replica set so I have 4 nodes.
127.0.0.1:27017
127.0.0.1:27018
127.0.0.1:27019
127.0.0.1:27020
And 27017 is selected as primary node. Then it fails and other nodes elect 27020 as primary node.
If my application was connecting to server on port 27017 as a primary node, and obviously it lost connection due to node failure, what should I do in my application? Try reconnecting on different port? Connect to any other node and get address of primary node and reconnect to new primary node?
Answer: drivers have support for replica
The arbiter is a mongodb server that runs on a third machine that helps negitiate between the paired servers to determine which one should be master.
Following is the PHP example code to connect when using a replica pair:
$m = new Mongo("mongodb://localhost:27017,localhost:27018");
I tested this by stopping one server at a time and the other took over as master and the application continued to work without any failures or unavailability.