Showing posts with label MongoDB. Show all posts
Showing posts with label MongoDB. Show all posts

Wednesday, August 9, 2017

How to check mongo process ID? How to check mongo status?

How to check mongo process ID:

ps -ax | grep -v grep | grep mongod
or
ps -aux | grep mongod
or

pgrep mongod

> 1017
--------------------------------------------------
Kill the process:

sudo kill -9 <PID>

Example:
sudo kill -9 1017
--------------------------------------------------
Check mongo Status:

Sudo systemctl status mongod
or
Sudo service mongod status

--------------------------------------------------
Start mongo service:

Sudo systemctl start mongod
or
Sudo service mongod start

--------------------------------------------------
Stop mongo service:

Sudo systemctl stop mongod
or
Sudo service mongod stop

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

RDBMS Where Clause Equivalents in MongoDB

RDBMS Where Clause Equivalents in MongoDB:


To query the document on the basis of some condition, you can use following operations.

Operation
Syntax
Example
RDBMS Equivalent
Equality {<key>:<value>} db.mycol.find({"by":"tutorials point"}).pretty() where by = 'tutorials point'
Less Than {<key>:{$lt:<value>}} db.mycol.find({"likes":{$lt:50}}).pretty() where likes < 50
Less Than Equals {<key>:{$lte:<value>}} db.mycol.find({"likes":{$lte:50}}).pretty() where likes <= 50
Greater Than {<key>:{$gt:<value>}} db.mycol.find({"likes":{$gt:50}}).pretty() where likes > 50
Greater Than Equals {<key>:{$gte:<value>}} db.mycol.find({"likes":{$gte:50}}).pretty() where likes >= 50
Not Equals {<key>:{$ne:<value>}} db.mycol.find({"likes":{$ne:50}}).pretty() where likes != 50

ResourceLink: https://www.tutorialspoint.com/mongodb/mongodb_query_document.htm

Why and Where to use MongoDB?

Why Use MongoDB?
1. Document Oriented Storage − Data is stored in the form of JSON style documents.
2. Index on any attribute
3. Replication and high availability
4. Auto-sharding
5. Rich queries
6. Fast in-place updates
7. Professional support by MongoDB

Where to Use MongoDB?
1. Big Data
2. Content Management and Delivery
3. Mobile and Social Infrastructure
4. User Data Management
5. Data Hub


Advantages of MongoDB

Advantages of MongoDB:

1. MongoDB enables horizontal scalability by using a technique called sharding. Sharding distributes the data across physical partitions to overcome the hardware limitations. The data is automatically balanced in the clusters.

2. It also provides ACID properties at the document level as in the case of relational databases.

3. It supports replica sets; in other words, a failover mechanism is automatically handled. If the primary server goes down, the secondary server becomes the primary automatically, without any human intervention.

4. It supports the common authentication mechanisms, such as LDAP, AD, and certificates. Users can connect to MongoDB over SSL and the data can be encrypted.

5. MongoDB can be a cost effective solution because improves flexibility and reduces cost on hardware and storage.


Advantages of MongoDB over RDBMS

The Advantages of MongoDB over RDBMS are


1. No schema migrations. Since MongoDB is schema-free, your code defines your schema.

2. Number of fields, content and size of the document can be different from one document to another.

3. Tuning using indexes

4. Ease of scale-out: MongoDB is easy to scale by adding commodity hardware

5. Conversion / mapping of application objects to database objects not needed since MongoDB uses object notation to represent data structures

6. Schemaless: MongoDB is document database in which one collection holds different documents.

7. Uses internal memory for storing the working set, enabling faster access of data

8. A document-based data model. The basic unit of storage is analogous to JSON, Python dictionaries, Ruby hashes, etc. This is a rich data structure capable of holding arrays and other documents.

9. A clear path to horizontal scalability.

10. Structure of a single object is clear and well defined

11. No complex joins required to bring together related data

12. Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL




What is dynamic schema in MongoDB?


Ans: 

Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.

Relationship of RDBMS terminology with MongoDB

The following table shows the relationship of RDBMS terminology with MongoDB.
RDBMS
MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Table Join Embedded Documents
Primary Key Primary Key (Default key _id provided by mongodb itself)
Database Server and Client
Mysqld/Oracle mongod
mysql/sqlplus mongo




  • Collection(Table) is a group of MongoDB documents(rows).
  • Documents(rows) within a collection(table) can have different fields(columns).
  • Document is a set of key-value pairs.

What is MongoDB?

MongoDB:

1. MongoDB is an open-source document database and leading NoSQL database.
2. MongoDB is written in C++.
3. MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability.
4. MongoDB works on concept of collection and document.


Wednesday, June 7, 2017

How to solve unmet dependencies?

macao@macao-pc:/tmp$ sudo apt-get install mongodb-org
Reading package lists... Done
Building dependency tree    
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 google-chrome-stable : Depends: libappindicator1 but it is not going to be installed
 mongodb-org : Depends: mongodb-org-shell but it is not going to be installed
               Depends: mongodb-org-server but it is not going to be installed
               Depends: mongodb-org-mongos but it is not going to be installed
               Depends: mongodb-org-tools but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).


Solution:


sudo apt-get install --fix-broken && sudo apt-get autoremove && sudo apt-get update 

Resource Link: https://askubuntu.com/questions/381894/problem-in-upgrading-linux-header-package-3-2-0-56-generic-pae-unmet-depend