970_90

Wednesday, 28 September 2016

MongoDB indexing and Embed document

MongoDB indexing and Embed document

Question: How to create indexing on single column?
db.users.ensureIndex({"title":1})
1- Indexing in Ascending Order.


Question: How to create indexing on multiple column?
db.users.ensureIndex({"title":1,"description":-1})
1- Indexing in Ascending Order.
-1- Indexing in Descendig Order.


Question: How to make unique key in MongoDB?
db.users.ensureIndex({"username":1},{unique:true,sparse:true})
Now you can't add duplicate username in users collection.


Question: What is sparse in MongoDB indexing?
Sparse indexes only contain entries for documents that have the indexed field. The index skips over any document that is missing the indexed field. The index is sparse because it does not include all documents of a collection. .


Question: How to create indexing in background process?
db.users.ensureIndex({"title":1},{background:true})



Question: How to create indexing on column which can have duplicate values?
db.users.ensureIndex({"title":1},{dropDups:true})



Question: How can we set index name while creating indexing?
db.users.ensureIndex({"title":1},{name:'bankdb_users_index'})



Question: How to drop an index on single column?
db.users.dropIndex({"title":1})



Question: How to drop all indexes on document?
db.users.dropIndexex()



Question: How to update all index in collection?
db.collection.reIndex()



Question: How to create indexing in Embed document?
db.users.ensureIndex({"profile.city":1})

Search Document From Embed document
db.users.find( { "profile.city" : "newYork" } )



Question: Compare "Multiple collections" vs "Embedded documents"?
  1. No much differences for insertion and updates document.
  2. Separate collections are better when you need to select individual documents and gives more control over querying.
  3. Embedded documents are better when embed document is less OR No More.
  4. Getting record from embedded documents is easy as compare Multiple collections.
  5. Embed document limit is 16MB
Question: What is Relationships in MongoDB?
Relationships denotes how various documents are logically related to each other.


Question: What are different ways to achieve the relationships in MongoDB?
Following are various two Approaches
1) Embedded Approaches 2) Referenced Approaches


Question: Given an example of Embedded?
[
  {
    "_id": ObjectId("52ffc33cd85242f436099901"),
    "contact": "987654321",
    "dob": "01-01-1991",
    "name": "Tom Benzamin",
    "address": [
      {
        "pincode": 123456,
        "city": "Los Angeles",
        "state": "California"
      },
      {       
        "pincode": 456789,
        "city": "Chicago",
        "state": "Illinois"
      }
    ]
  }
]



Question: Given an example of Referenced?
[
  {
    "_id": ObjectId("52ffc33cd85242f436099901"),
    "contact": "987654321",
    "dob": "01-01-1991",
    "name": "Tom Benzamin",
    "address": [
        ObjectId("63ffc4a5d85242602e000000"),
        ObjectId("63ffc4a5d85242602e000001")
    ]
  }
]



Tuesday, 27 September 2016

MongoDB Questions and Answers for beginners

MongoDB Questions and Answers for beginners

Question: What kind of NoSQL database used in MongoDB?
MongoDB is a document oriented database. It store data in form of BSON structure.


Question: What is a Namespace in MongoDB?
Namespace is concatenation of the database name and collection name.


Question: Does MongoDB support foreign key constraints?
No.


Question: Does MongoDB support ACID transaction?
No.


Question: Does MongoDB support Indexes?
Yes.


Question: Does MongoDB support primary key & foreign key relationships in MongoDB?
No, By Default it does not support. But we can achieve this by using embedding one document inside another.


Question: How is MongoDB better than other SQL databases?

Namespace is concatenation of the database name and collection name.
  1. Highly flexible
  2. Scalable Document
  3. Fast
  4. Replication



Question: When we removed an document. Does it removed permanently?.
Yes, When we rmoved and document it delete from disk.


Question: When we removed an document. Does it removed permanently?.
Yes, When we rmoved and document it delete from disk.


Question: What happens if an index does not fit into RAM?.
In that case MongoDB reads data from disk which is relatively very much slower.


Question: What is a covered query in MongoDB?.
Covered query is the one in which, A) fields used in the query are part of an index
AND
B) the fields returned in the results are in the same index.


Question: What is Aggregation in MongoDB??.
Aggregation is process of data-records and return computed results.


Question: What is Sharding in MongoDB??.
Sharding is a method for storing data across multiple machines (2 OR more machines).


Question: What is Replication in MongoDB?.
Replication is a process of synchronizing data across multiple servers.


Question: What are Primary and Secondary Replica sets?
Primary and master nodes are the nodes that can accept writes.
Secondary and slave nodes are read-only nodes that replicate from the primary.


Question: Why are MongoDB data files large in size?
MongoDB preallocates data files to reserve space and avoid file system fragmentation when you setup the server.


Question: What is a Storage Engine in MongoDB?
A storage engine is responsible for managing data like how data is stored on disk.


Question: What are two storage engines used by MongoDB?
1) MMAPv1
2) WiredTiger


Question: Why MongoDB is not preferred over a 32-bit system??
In 32-bit build system the total storage size for the server including data and indexes is 2GB.


Question: What is the role of a profiler in MongoDB??
The Profiler collects fine grained data about MongoDB write operations, cursors, database commands.


Question: How does MongoDB provide concurrency?
MongoDB uses reader-writer locks for the concurrency.


Question: What is hotfix for windows?
A hotfix is single/cumulative package to fix an issue.


Question: How to changes in storage path of MongoDB?
mongod --dbpath "d:\mongodb\data"



Question: How to get mongoDB Version ?
mongod --version