Вы находитесь на странице: 1из 4

Mongo DB Documentation

1. Create Database
use mydb
2. Show current Database
db
3. Show all Databases
show dbs
4. Drop Database
use mydb
Switched to db mydb
db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }
5. Create Collection (table)
use mydb
Switched to db mydb
db.createCollection(mycollection)
{ ok : 1 }
6. Insert a value
db.mycollection.insert( { name: sudhar, age :
23, groups: [news,sports] } )
or

Create two documents named j and k by using the following


sequence of JavaScript operations:

j = { name : "mongo" }
k={x:3}
db.testData.insert( j )
db.testData.insert( k )

7. Show the all collection (tables)


show collections
8. show the collection and value
db.mycollection.find()

9. Query for Specific Documents (table value)


db.mycollection.find( { x : 18 } )
or
db.mycollection.find( { x : 18 } ).preety()
10.

Drop Collection (Drop Table)


use mydb
db.mycollection.drop()

11.

Update Tables
db.users.update( { 'name' : 'sudharshanan'},
{$set: {'age' : 22 }})

12.

Update a table multiple value


db.mycollection.update( { 'name' :
'sudharshanan'}, {$set: {'age' : 22 }} ,
{ multi:true })

13.

Deleting Table Value (single value)


db.mycollection.remove({name : sudhar})

14.

Truncate a table
Db.mycollection.remove()

15. CreateUser for 2.2 Version


db.createUser(
{
user: "demo",
pwd: "demo",
roles: [ ]
}
)

15.
For 2.4 version Example
db.addUser( { user: "coresuser1_2",
pwd: "welcome",
roles: [ "read" ]
})
16.
Mongo DB Restore from different name
mongorestore --db=AMCFLogs
--username=amcfsumtwov2 --password=amcf2015
STLogs
17.

Login mongo db using credential

mongo -u mongouser -p Welcome123


--authenticationDatabase admin
Mongo DB Data Types
MongoDB supports many datatypes whose list is given
below:
String : This is 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 documents ID.
Binary data : This datatype is used to store binay data.
Code : This datatype is used to store javascript code into document.
Regular expression : This datatype is used to store regular expression

Вам также может понравиться