In this video tutorial we shall learn more about ObjectId( _id ) of MongoDB document.
ObjectId is a primary key in any mongoDB document. We can’t change document id once it has been created and inserted. If we do not explicitly assign value to _id, then it’ll be automatically assigned and inserted. We can’t have same document id for more than 1 document.
These ObjectId’s are of BSON datatype. It’s a 12-byte value.
Basically, each _id is a combination of .. – Time the document/record was saved/inserted – Host name of the machine/server it’s running on – Process id of the server process – and a random incremental number
Example documents
Since the ObjectId‘s are formed taking time of insertion of document into consideration, we can extract this time using getTimestamp() method.
1
db.info.find()[0]._id.getTimestamp()
db.info.find()[0]._id.getTimestamp()
Custom _id values We can explicitly define the _id value and in such case system won’t add it for us.
In this video tutorial, we’ll look at the basic differences between MongoDB and traditional Database Management Systems. Also, commands to create database, collection, documents – insertion.
Topic of discussion: Key differences. Keyword differences. Schema-free collection. Database creation. Creation of Collection. Insertion of Documents. JavaScript Console/Shall.
Some Commands
Show Database Already Present
1
show dbs
show dbs
Create Database
1
use company
use company
In MongoDB, it’ll never actually create any database or collection until we start storing documents in it!
Point object ‘db’ to our new database, by switching to company database
1
2
3
4
>use company
Switched to db company
>db
company
>use company
Switched to db company
>db
company
Naming our Collection
1
db.info
db.info
So our collection name is info
Number of documents present
1
db.info.count()
db.info.count()
using count() method, we check the no of documents present inside info collection.
We create a JSON object data. We start storing { key: value } pair into it. emp is an array, which contains name of 2 employees. videos is a sub object.
Now using save method we insert this data object into info collection.
Save() Save() is a higher lever method. It checks to see if the object we’re inserting is already present in our collection, by checking the _id value we’re inserting. _id field(which is a primary key in MongoDB document) – If there is no matching _id field in a document, it’ll call insert method and insert the data. And if the data being entered has no _id value, insert() method will create _id value for us, and insert the data into the collection. – If _id matches to any document in our collection, it’ll use update() instead to update the previously present data with the new data entered by the user.
Note: There is no Schema for our collection. i.e., no column name and datatype. Since documents are in BSON format, insertion and finding data is much faster.
Schema-free design is flexible and is of much use in modern day web application.
Example: Users upload photos and tag names of people in the pic. This can be stored as an array of tags in a key value pair, for only those pics which has tags. For other pics, we need not create this array itself. This is flexible. Also BSON format helps create application with high Scalability.
NoSQL is some what misleading name, which now means Not Only SQL!
The basic intention for NoSQL database is to scale modern web databases and to facilitate solving the complex data storage( and structuring ) requirements of modern day web applications.
MongoDB is a Document-Oriented Database System and is Schema-free. Here the Document is a BSON document. BSON is Binary JSON.
JSON stands for JavaScript Object Notation – It’s a fat-free alternative to XML. – JSON is a lightweight data-interchange format.
MongoDB and BSON MongoDB can be considered as a giant array of JSON objects, since the documents are in binary JSON format, insertion and searching will be much faster.
MongoDB – is Schema-Free – There is no predefined structure. i.e., There’ll be no column name and predefined datatype.
Downloading and Installing: MongoDB Goto MongoDB official website, click on Downloads link and select the 64-bit version for your OS and download it. Note that, 32-bit version has some limitations. Look for the MongoDB official documentations for more details.
Once you have the download file, unzip it.
If you’re on Windows OS, then create a folder C://data/db which will be used by MongoDB software.
To start using MongoDB, goto MongoDB folder, inside Bin, you can find mongod.exe – click on it, and leave it to run. Next click on mongo.exe and start passing the instructions/commands.
Note: mongo.exe is a JavaScript shall / console, so we’ll be writing JavaScript to work directly with mongodb database.
This video tutorial illustrates searching, installing, updating and removing the external, free modules.
External Free Node Modules are listed at npmjs.org and is managed by NPM – Node Package Manager.
There are so many amazing modules already written to handle so many sophisticated functionalities, that it becomes easy to use them in our application and build a robust, scalable application quickly.
Command to look at all the external module names and their respective description
1
npm search
npm search
It gives a long list of names and descriptions.
To make is look better, you can use GREP(if you’re on Linux or Unix based System) Search with GREP
1
npm search | grep module_name
npm search | grep module_name
npm search followed by pipe symbol, which is followed by any module name.
For Windows users
1
npm search module_name
npm search module_name
it gives all the details of available methods, properties and their description.
Installation of Modules
init command
1
npm init
npm init
Once you run this command, it prompts you with some optional things, like, name of the project, version, author, licensing etc. Once you’re done with that, it creates package.json file with all the details you provided.
The ‘init’ command in NPM allows us to identify our project and list out any Node Modules it requires.
Now open that package.json file: it looks somewhat like this package.json file
You need to open this package.json file using a plain text editor and add your project dependencies i.e., some of the modules you need to install for your project.
In this video tutorial we’re installing 3 external modules: express jade mongoose
What is a module? A module is a self-contained series of one or more javascript files represented by an object.
Update Command
1
npm update
npm update
once you run this command, package.json file is parsed and the dependency modules are checked for any updates, if any, it’ll be updated. If any module is installed with -g (global scope), i.e., the root installation, then while updating or removing them, it may throw some errors, in such case, use sudo keyword:
Update Command
1
sudo npm update -g
sudo npm update -g
This would solve the problem both for updating and deleting external modules.
remove/delete module command
1
npm prune
npm prune
To remove the unused modules: open package.json file and remove the module names which you want to remove. Save the package.json file and then run above(npm prune) command. This would safely remove the modules you no more intend to use in your project.
List Global module: using command
1
npm -g ls
npm -g ls
This lists all the global modules, it’s path and file structure: as shown below:
exports is a global provided by node.js students is a name given by us; it’s a property name and we assign an array to it which contains some names.
Application File appStu.js
1
2
3
var students = require('./students');
console.log(students.students);
var students = require('./students');
console.log(students.students);
We require the local module students in appStu.js file. ./ (dot followed by a forward slash) specifies that it’s a local module and it need not search for the file in core module or node module collection.
Next, object.property name will print out the entire array present inside the local module file.