Thursday, June 8, 2017

MongoDB commands step by step

MongoDB commands:

sudo service mongodb start
sudo service mongodb stop
sudo service mongodb restart

sudo systemctl start mongodb
sudo systemctl status mongodb
sudo systemctl stop mongodb

To use MongoDB run the following command.
$ mongo

To show the database name, number of collection and documents in the database. 
$ db.stats()

If you want to create a database with name <sys>, then use DATABASE statement would be as follows − 
> use sys
switched to db sys

> db.stats()
{
"db" : "sys",
"collections" : 1,
"objects" : 3,
"avgObjSize" : 216.66666666666666,
"dataSize" : 650,
"storageSize" : 16384,
"numExtents" : 0,
"indexes" : 1,
"indexSize" : 16384,
"ok" : 1
}
> db
sys
> show dbs
admin  0.000GB
local  0.000GB
sys    0.000GB
user   0.000GB
users  0.000GB

> db.games.insert({"name":"football"})
WriteResult({ "nInserted" : 1 })

In MongoDB default database is test. If you didn't create any database, then collections will be stored in test database.

> db.dropDatabase()
This will delete the selected database. If you have not selected any database, then it will delete default 'test' database.

If you want to delete new database <sys>, then dropDatabase() command would be as follows 
> use sys
switched to db sys

Drop Database command:

> db.dropDatabase()
> { "dropped" : "sys", "ok" : 1 }

> show dbs
admin  0.000GB
local  0.000GB
user   0.000GB
users  0.000GB

Table creation command:

> db.createCollection("mycol", { capped : true, autoIndexId : true, size :
... 6142800, max : 10000 } )
{
"note" : "the autoIndexId option is deprecated and will be removed in a future release",
"ok" : 1
}

Drop table/collections command:

> db.mycollection.drop()
true

Show all tables:

> show collections
api_flows
games
mycol

Insert data into mycol table.
> db.mycol.insert({id:'7df78ad8902c',title: 'MongoDB Overview', description: 'MongoDB is no sql database',by: 'tutorials point',url: 'http://www.tutorialspoint.com',tags: ['mongodb', 'database', 'NoSQL'],likes: 100})

> db.mycol.insert({id:'dskfl456ldjk34',title: 'DynamoDB Overview', description: 'DynamoDB is no sql database',by: 'AWS',url: 'http://www.aws.com',tags: ['dynamodb', 'database', 'NoSQL'],likes: 230})
WriteResult({ "nInserted" : 1 })
>

Show data from mycol table into structured way:

> db.mycol.find().pretty()
{
"_id" : ObjectId("5938e67a8e351ecde46646a6"),
"id" : "7df78ad8902c",
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials point",
"url" : "http://www.tutorialspoint.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}

Show data from mycol table into non-structured way:

> db.mycol.find()
{ "_id" : ObjectId("5938e67a8e351ecde46646a6"), "id" : "7df78ad8902c", "title" : "MongoDB Overview", "description" : "MongoDB is no sql database", "by" : "tutorials point", "url" : "http://www.tutorialspoint.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
>

Select last one from query using findOne():

> db.mycol.findOne()
{
"_id" : ObjectId("5938e67a8e351ecde46646a6"),
"id" : "7df78ad8902c",
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials point",
"url" : "http://www.tutorialspoint.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}

AND Query expression in MongoDB:

> db.mycol.find({$and:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty()
{
"_id" : ObjectId("5938e67a8e351ecde46646a6"),
"id" : "7df78ad8902c",
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials point",
"url" : "http://www.tutorialspoint.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>

Using AND & OR get expression together in MongoDB:

> db.mycol.find({"likes": {$gt:10}, $or: [{"by": "tutorials point"},
...    {"title": "MongoDB Overview"}]}).pretty()
{
"_id" : ObjectId("5938e67a8e351ecde46646a6"),
"id" : "7df78ad8902c",
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials point",
"url" : "http://www.tutorialspoint.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>

Resource Link: https://www.tutorialspoint.com/mongodb/mongodb_create_database.htm

No comments:

Post a Comment