MongoDB

MongoDB

To be able experiment with the code examples, you will need access to a MongoDb database.
You can download a free MongoDb database at https://www.mongodb.com.


how to install mongodb by using command line


Download and install mongodb package:

Now you have downloaded and installed a mongodb database driver.
Node.js can use this module to manipulate MongoDB databases:



  
In this chapter, we will see how to create a database in MongoDB.
The use Command

MongoDB use DATABASE_NAME is used to create database. The command will create a
new database if it doesn't exist, otherwise it will return the existing database.
Syntax
Basic syntax of use DATABASE statement is as follows:
use DATABASE_NAME
Example
If you want to create a database with name <mydb>, then use DATABASE statement
would be as follows:
>use mydb
switched to db mydb
To check your currently selected database, use the command db
>db
mydb
If you want to check your databases list, use the command show dbs.
>show dbs
local 0.78125GB
test 0.23012GB
Your created database (mydb) is not present in list. To display database, you need to
insert at least one document into it.
>db.movie.insert({"name":"tutorials point"})
 
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
In MongoDB default database is test. If you didn't create any database, then collections
will be stored in test database.

The dropDatabase() Method
MongoDB db.dropDatabase() command is used to drop a existing database.
Syntax
Basic syntax of dropDatabase() command is as follows:
db.dropDatabase()
This will delete the selected database. If you have not selected any database, then it will
delete default 'test' database.
Example
First, check the list of available databases by using the command, show dbs.
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
>
If you want to delete new database <mydb>, then dropDatabase() command would be as follows:
The createCollection() Method
MongoDB db.createCollection(name, options) is used to create collection.
Syntax
Basic syntax of createCollection() command is as follows:
db.createCollection(name, options) 

Basic syntax of createCollection() method without options is as follows:
>use test
switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 }
>
You can check the created collection by using the command show collections.
>show collections
mycollection
system.indexes
In MongoDB, you don't need to create collection. MongoDB creates collection
automatically, when you insert some document.
>db.tutorialspoint.insert({"name" : "tutorialspoint"})
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>

The drop() Method
MongoDB's db.collection.drop() is used to drop a collection from the database.
Syntax
Basic syntax of drop() command is as follows:
db.COLLECTION_NAME.drop()
Example
First, check the available collections into your database mydb.
>use mydb
switched to db mydb
>show collections
mycol
mycollection
system.indexes
tutorialspoint>
Now drop the collection with the name mycollection.
>db.mycollection.drop()




MongoDB  datatypes. Some of them are: 
String: This is the most commonly used datatype to store the data. String in
MongoDB must be UTF-8 valid.
 Integer: This type is used to store a numerical value. Integer can be 32 bit or 64
bit depending upon your server.
 Boolean: This type is used to store a boolean (true/ false) value.
Double: This type is used to store floating point values.
Min/Max Keys: This type is used to compare a value against the lowest and
highest BSON elements.
 Arrays: This type is used to store arrays or list or multiple values into one key.
Timestamp: ctimestamp. This can be handy for recording when a document has
been modified or added.
Object: This datatype is used for embedded documents.
Null: This type is used to store a Null value
 Symbol: This datatype is used identically to a string; however, it's generally
reserved for languages that use a specific symbol type
 Date: This datatype is used to store the current date or time in UNIX time format.
You can specify your own date time by creating object of Date and passing day,
month, year into it.
 Object ID: This datatype is used to store the document’s ID.
Binary data: This datatype is used to store binary data.
Code: This datatype is used to store JavaScript code into the document.
Regular expression: This datatype is used to store regular expression.

Comments

Popular posts from this blog

How to build a Wall Mounted Family Calendar and Dashboard

built web-app Authentication in React Applications

Secure, Simple and Scalable Video Conferencing with Jitsi