Install MongoDB on Windows
Download from http://www.mongodb.org/downloads
Start Server:
Go to C:\Windows\System32\cmd.exe
Open cmd
In the command prompt, execute the command
mongo or mongo.exe
Connect to MongoDB
Once you have installed Compass and the
program is running, an initial connection dialog appears:
In MongoDB, the first basic step is to have a database and collection in
place. The database is used to store all of the collections, and the
collection in turn is used to store all of the documents. The documents
in turn will contain the relevant Field Name and Field values.
Creating a database using “use” command
- The first part of the command is the "insert statement" which is the statement used to insert a document into the collection.
- The second part of the statement is to add the Field name and the Field value, in other words, what is the document in the collection going to contain.
printing in JSON format
Search records:{EmployeeName : "Devayan"}
MongoDB Cursor
When the db.collection.find () function is used to
search for documents in the collection, the result returns a pointer to
the collection of documents returned which is called a cursor.
MongoDB Query Modifications using limit(), sort()
- Limits
Example:
db.EmployeeDB.find().limit(2).forEach(printjson);
- Orders
Example:
db.EmployeeDB.find().sort({Employeeid:-1}).forEach(printjson)
Here the -1 indicates that we want to return the documents based on the descending order of Employee id.
MongoDB provides the count() function to the count of documents in a collection as per the query fired.
Example:
db.EmployeeDB.count()
In MongoDB, the db.collection.remove () method is used to remove documents from a collection.
Eample:
db.EmployeeDB.remove({Employeeid:'4'})
MongoDB provides the update() command to update the documents of a collection.
Example:
db.EmployeeDB.update({Employeeid:'3'},{$set:{"EmployeeName":"Anand"}})
Note:Please make sure that column name is correct,otherwise new column will be created.
Example:
db.EmployeeDB.update
(
{
Employeeid : '2'
},
{
$set :
{
"EmployeeName" : "NewMartin",
"Employeeid" : 22
}
}
)
Start Mongo Server
C:\Windows\System32\cmd.exe > mongod
Open mongo shell
mongo
-------------------
Show created databases
show dbs
--------------------
Show collections
use Student
show collections
------------------------
Create user with role as userAdminAnyDatabase who can access any database
db.createUser(
{ user: "mongodb",
pwd: "mongodb",
roles:[{role: "userAdminAnyDatabase" , db:"admin"}]})
--------------------------------------
create database Student
-------------------------------
//Display Records
db.StudentRecord.find().forEach(printjson)



0 comments:
Post a Comment