Mongodb - Basic stuffs

mongodb

How can we launch the mongo shell?

cd /installation/directory
./mongo

How can we quit the mongo shell?

quit();

How can we display a list of databases / schemas?

show dbs;

How can we switch to a different schema?

use schemaName;

How can we display a list of available collections?

show collections;
db.getCollectionNames()

How can we find a record?

db.collectionName.find({columnName: columnValue});

How can we pretty print?

db.collectionName.find({columnName: columnValue}).pretty();

How can we specify a list of fields to be returned / displayed?

db.collectionName.find({...},{desiredColumnName1: 1, desiredColumnName2: 1, ...})

How can we print without formating?

db.collectionName.find({columnName: columnValue}).print();

How can we print as JSON?

db.collectionName.find({columnName: columnValue}).printjson();

What is MongoDB?

MongoDB is a document-oriented database and each document has its own structure. Unlike a RDBMS in which each record must conform to the structure of its table, each document in MongoDB can have a different structure; you don’t have to define a schema for documents before saving them in the database.

What is the definition of Document?

A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents.

What is the format of a document?

MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents, though it contains more data types than JSON. For the BSON spec, see bsonspec.org. MongoDB documents are composed of field-and-value pairs and have the following structure:

{
   field1: value1,
   field2: value2,
   field3: value3,
   ...
   fieldN: valueN
}

The value of a field can be any of the BSON data types, including other documents, arrays, and arrays of documents. For example, the following document contains values of varying types:

var mydoc = {
               _id: ObjectId("5099803df3f4948bd2f98391"),
               name: { first: "Alan", last: "Turing" },
               birth: new Date('Jun 23, 1912'),
               death: new Date('Jun 07, 1954'),
               contribs: [ "Turing machine", "Turing test", "Turingery" ],
               views : NumberLong(1250000)
            }

The above fields have the following data types:

  1. _id holds an ObjectId.
  2. name holds an embedded document that contains the fields first and last.
  3. birth and death hold values of the Date type.
  4. contribs holds an array of strings.
  5. views holds a value of the NumberLong type.

What are the restrictions on the field names?

Documents have the following restrictions on field names:

  1. The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array.
  2. The field names cannot start with the dollar sign ($) character.
  3. The field names cannot contain the dot (.) character.
  4. The field names cannot contain the null character.

BSON documents may have more than one field with the same name. Most MongoDB interfaces, however, represent MongoDB with a structure (e.g. a hash table) that does not support duplicate field names. If you need to manipulate documents that have more than one field with the same name, see the driver documentation for your driver.

Some documents created by internal MongoDB processes may have duplicate fields, but no MongoDB process will ever add duplicate fields to an existing user document.

What are the limit on the field value?

For indexed collections, the values for the indexed fields have a Maximum Index Key Length limit.

What are the limits on documents?

  1. The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API. See mongofiles and the documentation for your driver for more information about GridFS.
  2. MongoDB preserves the order of the document fields following write operations except for the following cases:
    1. The _id field is always the first field in the document.
    2. Updates that include renaming of field names may result in the reordering of fields in the document.
    3. Starting in version 2.6, MongoDB actively attempts to preserve the field order in a document. Before version 2.6, MongoDB did not actively preserve the order of the fields in a document.

What are the limitations on the _id field?

n addition, if the mongod receives a document to insert that does not contain an _id field (e.g. through an update operation with an upsert option) mongod will add the _id field that holds an ObjectId. The _id field has the following behavior and constraints:

  1. By default, MongoDB creates a unique index on the _id field during the creation of a collection.
  2. The _id field is always the first field in the documents. If the server receives a document that does not have the _id field first, then the server will move the field to the beginning.
  3. The _id field may contain values of any BSON data type, other than an array.
  4. To ensure functioning replication, do not store values that are of the BSON regular expression type in the _id field.

The following are common options for storing values for _id:

  1. Use an ObjectId.
  2. Use a natural unique identifier, if available. This saves space and avoids an additional index.
  3. Generate an auto-incrementing number.
  4. Generate a UUID in your application code. For a more efficient storage of the UUID values in the collection and in the _id index, store the UUID as a value of the BSON BinData type.
  5. Index keys that are of the BinData type are more efficiently stored in the index if:
    1. the binary subtype value is in the range of 0-7 or 128-135, and
    2. the length of the byte array is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, or 32.
  6. Use your driver’s BSON UUID facility to generate UUIDs. Be aware that driver implementations may implement UUID serialization and deserialization logic differently, which may not be fully compatible with other drivers. See your driver documentation for information concerning UUID interoperability.

Most MongoDB driver clients will include the _id field and generate an ObjectId before sending the insert operation to MongoDB; however, if the client sends a document without an _id field, the mongod will add the _id field and generate the ObjectId.

What is the significance of the dot notation?

MongoDB uses the dot notation to access the elements of an array and to access the fields of an embedded document.

How can we access a member of an array by the zero-based index position?

To specify or access an element of an array by the zero-based index position, concatenate the array name with the dot (.) and zero-based index position, and enclose in quotes:

"<array>.<index>"

For example, given the following field in a document:

{
   ...
   contribs: [ "Turing machine", "Turing test", "Turingery" ],
   ...
}

To specify the third element in the contribs array, use the dot notation "contribs.2".

What is the definition of a collection?

MongoDB groups document objects into collections. You can think of a collection as a table like you would create in a RDBMS, but the difference as I said before is that they won’t force you to define a schema before you can store something.

MongoDB stores documents in collections. Collections are analogous to tables in relational databases. Unlike a table, however, a collection does not require its documents to have the same schema. https://docs.mongodb.com/getting-started/shell/introduction/

What is the definition of a database in MongoDB?

In MongoDB, databases hold collections, and each collection hold BSON documents.

How can we switch between different databases?

Use the use statement:

use databaseName

How can we create a database?

If a database does not exist, MongoDB creates the database when you first store data for that database. Therefore, you can switch to a non-existent database and perform the following operation in the mongo shell:

use databaseName
db.collectionName.insert( { x: 1 } )

How can we create a collection?

If a collection does not exist, MongoDB creates the collection when you first store data for that collection:

db.createCollection(collectionName, [options]);
db.collectionName.insert( { x: 1 } )
db.collectionName.createIndex( { y: 1 } )

Both the insert() and the createIndex() operations create their respective collection if they do not already exist. MongoDB provides the db.createCollection() method to explicitly create a collection with various options, such as setting the maximum size or the documentation validation rules. If you are not specifying these options, you do not need to explicitly create the collection since MongoDB creates new collections when you first store data for the collections. To modify these collection options, see collMod.

Does MongoDB require documents to have the same schema?

No. By default, a collection does not require its documents to have the same schema; i.e. the documents in a single collection do not need to have the same set of fields and the data type for a field can differ across documents within a collection. Starting in MongoDB 3.2, however, you can enforce document validation rules for a collection during update and insert operations. See Document Validation for details.

Does MongoDB require that documents in a single collection to have the same set of fields and datatype?

No. By default, a collection does not require its documents to have the same schema; i.e. the documents in a single collection do not need to have the same set of fields and the data type for a field can differ across documents within a collection. Starting in MongoDB 3.2, however, you can enforce document validation rules for a collection during update and insert operations. See Document Validation for details.

How can we modify document structure?

To change the structure of the documents in a collection, such as add new fields, remove existing fields, or change the field values to a new type, just update the documents to the new structure (use the update statement).

Can we have nested-documents?

Yes. With MongoDB, you can embed a document inside another one, which is really useful for cases where there is a one-to-one relationship. In a typical RDBMS you’d need to create two tables and link them together with a foreign key to achieve the same result. MongoDB doesn’t support joins, which some people see as a con. But if you organize your data correctly then you’ll find you don’t need joins, which is a pro since you’ll benefit from very high performance. https://www.sitepoint.com/introduction-to-mongodb/

Does MongoDB have joins?

No. With MongoDB, you can embed a document inside another one, which is really useful for cases where there is a one-to-one relationship. In a typical RDBMS you’d need to create two tables and link them together with a foreign key to achieve the same result. MongoDB doesn’t support joins, which some people see as a con. But if you organize your data correctly then you’ll find you don’t need joins, which is a pro since you’ll benefit from very high performance. https://www.sitepoint.com/introduction-to-mongodb/

What are the strength and weakness of NoSQL versus RDBMS?

It’s worth mentioning the aim of MongoDB and NoSQL isn’t to kill off RDBMS. RDBMSs are still a very good solution for most of the development world’s needs, but they do have their weaknesses, most noticeably the need to define a rigid schema for your data which is one problem NoSQL tries to solve. https://www.sitepoint.com/introduction-to-mongodb/

The advantages of using documents are:

  1. Embedded documents and arrays reduce need for expensive joins.
  2. Support for embedded data models reduces I/O activity on database system.
  3. Indexes support faster queries and can include keys from embedded documents and arrays.
  4. Documents (i.e. objects) correspond to native data types in many programming languages.
  5. Dynamic schema supports fluent polymorphism.

What is the recommended way to install Mongo?

  • Use the package manager that comes with your operating system
  • Download the appropriate package from http://www.mongodb.org/

You can download the compressed file of the latest version from its website and unpack it on your server. Then, create a directory for the database and run the mongod binary with the path to your database directory.

tar zxf mongodb-<version>.tgz
mv mongodb-<version> mongodb
mkdir mongodb/data
mongodb/bin/mongod --dbpath mongodb/data &

How can we start the MongoDB database server?

mongodb/bin/mongod --dbpath mongodb/data &

How can we start mongodb?

mongod --replSet meteor
mongod --dbpath=/data --port 27017

How can we install MongoDB on Mac OSX?

brew update
brew install mongodb --with-openssl

How can we start the Mongo shell?

Fire up a terminal and type in mongo.

While using the Mongo shell, how can we get help?

Type help in the mongo shell for a list of available commands and their descriptions:

help

The mongo shell also provides <tab> key completion as well as keyboard shortcuts similar to those found in the bash shell or in Emacs. For example, you can use the <up-arrow> and the <down-arrow> to retrieve operations from its history.

How can we import a dataset?

We must have a running mongod instance in order to import data into the database.

  1. Retrieve the dataset from https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json and save to a file named primer-dataset.json
  2. In the system shell or command prompt, use mongoimport to insert the documents into the restaurants collection in the test database. If the collection already exists in the test database, the operation will drop the restaurants collection first.
mongoimport --db test --collection restaurants --drop --file ~/downloads/primer-dataset.json

The mongoimport connects to a mongod instance running on localhost on port number 27017. The —file option provides the path to the data to import, in this case, ~/downloads/primer-dataset.json.

To import data into a mongod instance running on a different host or port, specify the hostname or port by including the —host and the —port options in your mongoimport command.

What is the default port number that MongoDB use?

27017

How can we create a database with Mongo?

Initially, no database is created. But don't worry, they'll instantly be created when we start inserting our records:

db.nettuts.insert({
    first: 'matthew',
    last: 'setter',
    dob: '21/04/1978',
    gender: 'm',
    hair_colour: 'brown',
    occupation: 'developer',
    nationality: 'australian'
});

I think that the above code create the nettuts collection.

db.nettuts.find()

The id field is auto generated by Mongo for you, if you don't specify an id. The reason is that every record must have a unique id field.

What is the definition of the term 'selector'?

Selectors are to Mongo what where clauses are to SQL. Say that we want to find all actors that are female. To accomplish that, you'll need to run the following command:

db.nettuts.find({gender: 'f'});
db.nettuts.find({gender: 'm', $or: [{nationality: 'english'}]});
db.nettuts.find({gender: 'm', $or: [{nationality: 'english'}, {nationality: 'american'}]});

How can we drop a collection?

The remove all operation only removes the documents from the collection. The collection itself, as well as any indexes for the collection, remain. To remove all documents from a collection, it may be more efficient to drop the entire collection, including the indexes, and then recreate the collection and rebuild the indexes. Use the drop() method to drop a collection, including any indexes.

db.restaurants.drop()

How can we prevent unauthorized access?

Since we are using a separate MongoDB server, we need to prevent unauthorized access of it. We can configure a firewall or use MongoDB’s role-based access control. To make the set-up simple, I assume we’ve configured the firewall properly. If it is not possible to configure a firewall, try using MongoDB’s role-based access control.

What do I still have to do before I start using Mongo?

Purchase the latest book on Mongo and read it. I need to know the various challenges that people faced using it, and managing it. I need to know how to effectively use it, and manage it.

What is a sub-document?

A sub-document is a field having a document structure.

How does MongoDB provide High Availability?

MongoDB’s replication facility, called replica set, provides:

  1. automatic failover and
  2. data redundancy.

A replica set is a group of MongoDB servers that maintain the same data set, providing redundancy and increasing data availability.

How does MongoDB provide Horizontal Scalability?

MongoDB provides horizontal scalability as part of its core functionality:

  1. Sharding distributes data across a cluster of machines.
  2. Tag aware sharding allows for directing data to specific shards, such as to take into consideration geographic distribution of the shards.

How many storage engines does MongoDB support?

MongoDB supports multiple storage engines, such as:

  1. WiredTiger Storage Engine and
  2. MMAPv1 Storage Engine.

In addition, MongoDB provides pluggable storage engine API that allows third parties to develop storage engines for MongoDB. https://docs.mongodb.com/manual/introduction/

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License