Tuesday, January 8, 2019

Mongo DB note

1.
db.version()
MongoDB stores its information in documents rather than rows.
MongoDB’s data model is document-oriented.
Internally, MongoDB stores documents in a format called Binary JSON, or BSON. BSON
has a similar structure but is intended for storing many documents.

2.
Indexes in MongoDB are implemented as a B-tree data structure.
HBase, are considered keyvalue
stores because they don’t allow any secondary indexes. This is a significant feature
in MongoDB; by permitting multiple secondary indexes MongoDB allows users to optimize
for a wide variety of queries.

3.
Replica sets consist of many MongoDB servers, usually
with each server on a separate physical machine;
we’ll call these nodes.
a replica set’s primary node can accept both
reads and writes, but the secondary nodes are readonly.

Journaling is enabled by
default since MongoDB v2.0.
With journaling, every write
is flushed to the journal file every 100 ms. If the server is ever shut down uncleanly
(say, in a power outage), the journal will be used to ensure that MongoDB’s data files
are restored to a consistent state when you restart the server.

MongoDB was designed to make horizontal scaling manageable. It does so via a
range-based partitioning mechanism, known as sharding, which automatically manages the distribution of data across nodes.

All the data files for a mongod process are stored by default in
/data/db on Unix-like systems and in c:\data\db on Windows.
mongod --help

They (Cassandra) opt for eventual consistency, which means that reads don’t
necessarily reflect the latest write. But what users get in exchange for weaker consistency
is the ability to write in the face of any one node’s failure.

This contrasts with MongoDB, which provides strong consistency, a rich data
model, and secondary indexes. The last two of these attributes go hand in hand; keyvalue
stores can generally store any data structure in the value, but the database is
unable to query them unless these values can be indexed.

MongoDB queries are generally composed of JSON
objects rather than text strings as in SQL.

4.
databases in MongoDB are just namespaces
to distinguish between collections. To query MongoDB, you’ll need to know the database
(or namespace) and collection you want to query for documents.
Databases and collections
are created only when documents are first inserted.

5.
The find() command
automatically returns 20 documents—if they’re available—after iterating the
cursor 20 times.

6.
help()
db.help()
mongo --help

db.numbers.find({num: {"$gt": 19995}}).explain("executionStats")


show dbs
show collections
db.stats()
db.runCommand( {dbstats: 1} )
db.numbers.stats()

MongoDB also allows ad hoc queries, but joins aren’t
supported.
Products and categories are the mainstays of any e-commerce site

7.
Use aggregate and lookup to merge reference
specify validator to validate the document insert/update

8. Compound index

db.persons.createIndex({gender:1,"dob.age":1});
   db.persons.explain().find({gender:"male"}); will use index while by dob.age won't
db.persons.createIndex({"dob.age":1,gender:1});
  db.persons.explain().find({"dob.age":{$gt:60}}) will use index while gender won't
Same as Oracle compound index