Friday 20 May 2016

Background Index Creation in MongoDB





Lecture Notes

As of MongoDB 2.6, background index creation will occur on secondaries as well as
the primary when a background index is created on the primary of a replica set.

Text Indexes in MongoDB




Lecture Notes

In this lesson, Dwight uses ensureIndex.
This command has been deprecated in favor of createIndex.

Geospatial Indexes in MongoDB.


Lecture Notes

In this lesson, Dwight uses ensureIndex. This command has been deprecated in favor of createIndex

TTL Indexes in MongoDB.


Sparse Indexes in MongoDB



Lecture Notes

In this video, Dwight uses the ensureIndex command, which is now deprecated. You would now use createIndex.

Quiz

Quiz: Sparse Indexes

If an index is unique AND sparse, can 2 documents which do
not include the field that is indexed exist in the same collection?
Yes

No


Ans.

Yes

Unique Indexes in MongoDB


Lecture Notes
In this video, Dwight uses the ensureIndex command, which is now deprecated. You would now use createIndex.

Quiz

Quiz: Unique Indexes

If an index is created with the options document, { unique : true } can 2 documents which do not include the field that is indexed exist in the same collection?
Yes

No
Ans.

NO

Wednesday 4 May 2016

Index Notes in mongoDB


Lecture Notes
In this video, Dwight mentions that the index is used automatically if it is present. This is no longer always true if you are sorting using a sparse index, but remains true otherwise. He also uses the ensureIndex command, which is deprecated as of MongoDB 3.0 in favor of createIndex
Quiz: Index Notes
A MongoDB index can have keys of different types (i.e., ints, dates, string)
1.True
2.False


Ans.
True
Advance on Index Notes in MongoDB

Collection Scans in MongoDB


createIndex(), getIndexes() & dropIndex() functions in mongoDB


Lecture Notes
As of mongodb 3.0, ensureIndex() is deprecated, though it will still work; you should use createIndex() instead. If you use ensureIndex with mongodb 3.0, it will point to createIndex.
Quiz: createIndex(), getIndexes() & dropIndex()
What will happen if an index is created on a field that does not exist in any of the documents in the collection?
1.MongoDB will throw an error and not create the index.
2.MongoDB will create the index but give a warning.
3.MongoDB will not create the index and not throw an error.
4.MongoDB will ask if you're sure first.
5.MongoDB will create the index without any warning.


Ans.


5.MongoDB will create the index without any warning.

Storage Engine: WiredTiger in mongoDB



Quiz: Storage Engine: WiredTiger
In MongoDB 3.0, the WiredTiger storage engine brings which of the following to MongoDB? Check all that apply.
1.Document-level concurrency control
2.Compression
3.Replication


Ans.

1.Document-level concurrency control

2.Compression

Answer

The only incorrect answer is replication, which has been, and remains, available for MongoDB for all supported releases of the server.

MMAPv1: Documents and Data Files in mongoDB



Lecture Notes
At approximately 7:40, I say that document moves might leave 'holes in your document...' I meant holes in your data files.

Quiz: MMAPv1: Documents and Data Files

Relative to no padding, Power of Two Sized Allocations in MMAPv1 provides which of the following benefits? Check all that apply.
1.Document movement need no longer result in updates to your indexes.
2.Record spaces are likely to get re-used.
3.Documents will not have to move as soon as they grow in size.
4.Documents that grow at a constant rate will move less often as time goes on.

Ans.


2.Record spaces are likely to get re-used.
3.Documents will not have to move as soon as they grow in size.
4.Documents that grow at a constant rate will move less often as time goes on.

Answer
The following answers are correct:
Documents will not have to move as soon as they grow in size. - This is because you have some space to grow before you reach your record size.
Record spaces are likely to get re-used. - Without standardized sizes, the likelihood that your new document will want the same size of record space as an old document might be slim, especially if your documents have different sizes (which they will if they're all growing). With standard sizes, all documents are likely to find record spaces that fit them.
Documents that grow at a constant rate will move less often as time goes on. - This makes sense, if you think about it. If your documents start out at an average size of 20 bytes, and grow at a rate of 1 byte per day, your first move will happen in 12 days (since the smallest record size is 32 bytes). Your second move will take 32 days, and your third will take 64. Each successive move will take longer and longer.
The incorrect answer is:
Document movement need no longer result in updates to your indexes. - this is not true for MMAPv1.

MMAPv1: Documents and Data Files in mongoDB


Storage Engine: MMAPv1 in mongoDB



Quiz: Storage Engine: MMAPv1

To understand MMAPv1, it is important to know that it:
1.Maps data files directly into virtual memory.
2.Maps data coming through the socket directly onto disk.
3.Maps JSON directly to BSON.
4.Maps compressed data files directly into BSON.
5.Maps data files directly into physical memory.

Ans.
5.Maps data files directly into virtual memory.

Chapter- 3 Storage Engine: Introduction


The storage engine directly determines which of the following? Check all that apply.
1.The wire protocol for the drivers
2.Format of indexes
3.Architecture of a cluster
4.The data file format


Ans.
2.Format of indexes
4.The data file format
The storage engine handles the interface between the database and the hardware, and by hardware, we mean specifically memory and disk, and any representations of the data (or metadata) that is located there, which is to say the data and indexes.
The data file format - The data files' format is determined by the storage engine, so this choice is correct. Different storage engines can implement different types of compression, and different ways of storing the BSON for mongoDB.
Architecture of a cluster - We picked false here, but you might argue with this choice. The architecture of the cluster is determined by interactions between servers, which communicate over the wire. Since the storage engine doesn't affect the wire protocol, the server architecture could be exactly the same, which is why we went with false. Even so, one might argue that a good choice of storage engine should result in a smaller cluster to handle the same workload. If your gut was to go with true for this reason, that's a defensible choice.
The wire protocol for the drivers - False. The wire protocol determines how servers communicate with each other, or with the applications. This is clearly outside of the storage engine.
Format of indexes - True. It may not be obvious when you first think about it, but indexes are controlled by the storage engine. For instance, MongoDB uses Btrees. With MongoDB 3.0, WiredTiger will be using B+ trees, with other formats expected to come in later releases.

Saturday 30 April 2016

Insert Documents in mongodb.


Overview of chapter 2 commands in mongodb.


collection.starts() and collection.drop() command in mongodb.


db.currentop() and db.killop() command in mongodb


db.serverstatus() command in mongodb.


db.ismaster() command in mongodb.


db.run command in mongodb.


Command operators in mongodb.


Bulk wire operators in mongodb.


Wire protocol in mongodb.


Lecture Notes At around 2:09, Dwight specifies the 'basic building blocks' of the wire protocol, and includes Commands in that. Just to clarify Dwight's intention, Commands are not built into the wire protocol as basic building blocks, even though they are a basic conceptual building block.

Upserts in mongodb.


Lecture Notes When this video was originally recorded, for MongoDB 2.6, the format was: > db.collection.update( query_document , update_document , [ upsert_boolean , [ multi_boolean ] ] ) This is no longer the recommended format. We now recommend: > db.collection.update( query_document, update_document, options_document ) where options_document contains key:value pairs such as: multi : true/false, upsert : true/false, writeConcern: document

update operators in mongodb,


Delete doccument in mongodb.


Partial update in mongodb.


Update in mongodb doccuments.


Adminstartive command in mongodb


welcome to chapter-2


Wednesday 13 April 2016

Quiz and Homework of MongoDB

Quiz: Concepts

Q.>What were the big differences in hardware over the last few decades that MongoDB attempted to address?


1.Parallelism of cores
2.Parallelism of servers
3.Quantum computers
4.Faster clock speeds
5.More memory

Ans.

1.Parallelism of cores
2.Parallelism of servers


Quiz: Scaling


Q: When scaling out horizontally (adding more servers to contain your data), what are problems that arise as you go from, say, 1 commodity server to a few dozen?

1. The original server, if incorporated into the cluster, is more likely to fail in a given unit of time than if it had been left as a single server.
2. The servers must communicate with one another eating up network bandwidth
3. The need for redundancy increases as the likelihood of some failure in the system per unit of time increases.
4. Hardware cost per server is likely to increase.

Ans
2. The servers must communicate with one another eating up network bandwidth
3. The need for redundancy increases as the likelihood of some failure in the system per unit of time increases.


Quiz: SQL and Complex Transactions

Q: What causes significant problems for SQL when you attempt to scale horizontally (to multiple servers)?

1. Joins
2. Queries
3. Transactions
4. Inserts
5. Indexes


Ans.

1. Joins
3. Transactions


Quiz: Documents Overview

Q.> What are some advantages of representing our data using a JSON-like format?


1. JSON presents a flexible and concise framework for specifying queries as well as storing records.
2. JSON syntax is similar to that of common data structures used in many programming languages and is, therefore, familiar to developers.
3. JSON is language independent.
4. JSON is optimized for use with JavaScript.

Ans.

1. JSON presents a flexible and concise framework for specifying queries as well as storing records.
2. JSON syntax is similar to that of common data structures used in many programming languages and is, therefore, familiar to developers.
3. JSON is language independent.


Quiz: JSON Syntax
Quiz: JSON Types

Q.> How many data types are there in JSON?

a. 1 (just strings)
b. 2
c. 4
d. 6


Ans

d. 6



Q.> What is the corresponding JSON for the following XML document?

<person>
  <name>John</name>
  <age>25</age>
  <address>
    <city>New York</city>
    <postalCode>10021</postalCode>
  </address>
  <phones>
    <phone type="home">212-555-1234</phone>
    <phone type="mobile">646-555-1234</phone>
  </phones>
</person>


Ans

{
"name" : "John",
"age" : 25,
"address" : { "city" : "New York", "postalCode" : "10021" },
"phones" : [ {"phone":"212-555-1234", "type" : "home"}, {"phone":"646-555-1234", "type" : "mobile"} ]
 }



Quiz: JSON Syntax 2


Q.> For the following XML, Is the corresponding JSON example legal json?
<things>
  <hat>one</hat>
  <coat>z</coat>
  <hat>two</hat>
</things>
{
  "hat" : "one",
  "coat" : "z",
  "hat" : "two"
}

1. Yes
2. No
3. Maybe

Ans

3. Maybe



Quiz: Binary JSON


Q.> Why do we represent our data as BSON rather than JSON in the system?


  1. Fast machine scanability.
  2. Human readability.
  3. Stronger typing (and more types) than JSON

 Ans.

1. Fast machine scanability.
3. Stronger typing (and more types) than JSON




Quiz: BSON and applications


Q.> For a typical client (a python client, for example) that is receiving the results of a query in BSON, would we convert from BSON to JSON to the client's native data structures (for example, nested dictionaries and lists in Python), or would we convert from BSON straight to those native data structures?
  
  1.  BSON -> Native data structures
  2. BSON -> JSON -> Native data structures


 Ans.

  1. BSON -> Native data structures




Quiz: Dynamic Schema


Q.> True or False: MongoDB is schemaless because a schema isn't very important in MongoDB


  1. True
  2. False

 Ans. 

2. False



Quiz: What is the MongoDB shell?


Q: By default, which database does the mongo shell connect to?


  1. test
  2. localhost
  3. mongo
  4. help
  5. default

 Ans

  1. test




Quiz: Mongoimport


The mongoimport utility can import what types of data?


  1. JSON
  2. CSV
  3. BSON
  4. TSV
  5. XML

 Ans.

  1. JSON
  2. CSV
  3. BSON
  4. TSV



Quiz: Cursors Introduction


In order to query a collection in the mongo shell, we can type which of the following?


  1. db.collection.find()
  2. db.find()
  3. db.collection.get()
  4. db.collection.select(*)
  5. db.collection.get(true)



 Ans.

5. db.collection.find()



None: Query Language: Basic Concepts

Q.> You have a collection where every document has the same fields, and you want to look at the value of the “_id”, "name", and “email” fields in order to see their format. Furthermore, you want to eliminate all other fields from the query results. What query might you write?


  1. db.collection.find({ name : 1, email: 1 }, {} )
  2. db.collection.find( { } , { name: 1 , email: 1 } )
  3. db.collection.find( { name: 1 } , { email : 1 } )
  4. db.collection.find( {name: 1 , email: 1 } )

 Ans.

2. db.collection.find( { } , { name: 1 , email: 1 } )



Quiz: Query Language: Projection


Q.> You want to query the “people” collection, you want the results of the query to include only documents where age is 50, and you want to look at all fields except “email”. What query should you write?


  1. db.people.find( { email : 0, age : 50 } , { } )
  2. db.people.find( { email : 0 }, { age : 50 } )
  3. db.people.find( { age : 50 } , {email: 0 } )
  4. db.people.find( { } , { email : 0 , age : 50 } )
  5. db.people.find( { age : 50 }, {email : 1} )

 Ans.

3. db.people.find( { age : 50 } , {email: 0 } )



Quiz: Query Language: Advantages of a Dynamic Schema


Q.> If you want to add a new key: value pair to the documents in the “shapes” collection, what methods could you use?


  1. db.shapes.alterCollection()
  2. db.shapes.update()
  3. db.shapes.resizeObject()
  4. db.shapes.find()
  5. db.shapes.save()

 Ans.

2. db.shapes.update()
4. db.shapes.save()


Quiz: Sorting


Q.> If you want to run a query on the collection, “books,” and sort ASCIIbetically by title on the query, which of the following will work?


  1. db.books.find( { } , { sort : { title : 1 } } )
  2. db.books.find( { } , { title : "$sort" } )
  3. db.books.find( { } ).sort( { title : -1 } )
  4. db.books.sort( { title : 1 } ).find( { } )
  5. db.books.find().sort( { title : 1 } )
Ans.


5. db.books.find().sort( { title : 1 } )


Quiz: Query Language: Cursors


Q. > Recall the documents in the scores collection:
{
                "_id" : ObjectId("50844162cb4cf4564b4694f8"),
                "student" : 0,
                "type" : "exam",
                "score" : 75
}
Write a query that retrieves documents of type "exam", sorted by score in descending order, skipping the first 50 and showing only the next 20.


Ans 


db.scores.find( { type : "exam" } ).sort( { score : -1 } ).skip( 50 ).limit( 20 )




Homework: Homework 1.1

Download and install MongoDB from www.mongodb.org. Then run the database as a single server instance on your PC (that is, run the mongod binary). Then, run the administrative shell.

From the shell prompt type

 db.isMaster().maxBsonObjectSize
at the ">" prompt.

What do you get as a result?



Ans.

16777216




Homework: Homework 1.2

Download the handout. Take a look at its content.

Now, import its contents into MongoDB, into a database called "pcat" and a collection called "products". Use the mongoimport utility to do this.

When done, run this query in the mongo shell:

db.products.find( { type : "case" } ).count()
What's the result?



 Ans.
 3



Homework: Homework 1.3

At this point you should have pcat.products loaded from the previous step. You can confirm this by running in the shell:

db.products.find()
// or:
db.products.count()
// should print out "11"
Now, what query would you run to get all the products where brand equals the string “ACME”?



Ans.
db.products.find({"brand": "ACME"})




Homework: Homework 1.4

How would you print out, in the shell, just the value in the "name" field, for all the product documents in the collection, without extraneous characters or braces, sorted alphabetically, ascending? (Check all that would apply.)


1. var c = db.products.find( { } ).sort( { name : -1 } ); while( c.hasNext() ) { print( c.next().name); }
2. var c = db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } ); while( c.hasNext() ) { print( c.next().name); }
3. var c = db.products.find( { } ).sort( { name : 1 } ); c.forEach( function( doc ) { print( doc.name ) } );
4. db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } )




 Ans.

2. var c = db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } ); while( c.hasNext() ) { print( c.next().name); }
3. var c = db.products.find( { } ).sort( { name : 1 } ); c.forEach( function( doc ) { print( doc.name ) } );
4. db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } )








Quiz: Concepts

Quiz: Concepts

What were the big differences in hardware over the last few decades that MongoDB attempted to address?

Overview of MondoDB Advance, Administrative commands of mongoDB.


Query Language Cursors in MongoDB,


Sorting in mongoDB using shell query.


Example of MongoDB shell query.


Shell query of mongoDB,


Advantage of mongoDB query Language in dynamic schema.


Query projection in mongodb.


Query of mongoDB basics.


Cursors introductions in mongoDB


Import json and data in MongoDB


Introduction of MongoDb Shell


Dynamic schema in mongoDb


Dynamic BSON


Binary Json introduction


Json RFC Rule and replication in json


Json to XML Conversion


Data types in json


Installing MongoDB windows versions


Installing Mongodb on mac


Introduction of mongodb


Course overview for DBA


welcome to mongodb tutorials