Mongodb *~*~

Jamsheed P
Sep 6, 2018 · 3 min read

MongoDB is an open source, document-oriented database designed with both scalability and developer agility in mind. Instead of storing your data in tables and rows as you would with a relational database, inMongoDB you store JSON-like documents with dynamic schemas

  1. Install MongoDB

sudo apt-key adv — keyserver hkp://keyserver.ubuntu.com:80 — recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5

echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list

sudo apt-get update

sudo apt-get install -y mongodb-org


2.Run MongoDB
……………………………………………………
• sudo service mongod QUERY EXAMPLE start
• [initandlisten] waiting for connections on port <port>
• sudo service mongod QUERY EXAMPLE stop
• sudo service mongod QUERY EXAMPLE restart


3.Uninstall MongoDB
……………………………………………………
• sudo service mongod QUERY EXAMPLE stop
• sudo apt-get purge mongodb-org*
• sudo rm -r /var/log/mongodb
• sudo rm -r /var/lib/mongodb


Mongodb QUERY EXAMPLE

Step1. Open Terminal enter mongo

> show databases
> show dbs

Default Select Database Please Enter db

> db
test //default database name

Create New Database or switched Database

use.<newDataBaseName> or <OldDataBaseName>
eg
> use test_db
switched to db test_db

Create Table or Collection

> db.createCollection("mycollection")
{ "ok" : 1 }

Show Table or Collection

> show collections
mycollection

1.INSERT Value in DataBase

> db.mycollection.insert( { strName: "JAMSHEED", intAge: 28} )   WriteResult({ "nInserted" : 1 })

Show Insert data

> db.mycollection.find()Result  { "_id" : ObjectId("5b90f2d88618a9e724c017ec"), "strName" : "JAMSHEED", "intAge" : 28 }> db.mycollection.find().pretty()Result{
"_id" : ObjectId("5b90f2d88618a9e724c017ec"),
"strName" : "JAMSHEED",
"intAge" : 28
}

“_id” = Automatically create ObjectId ( this id is unique)

ObjectId(<hexadecimal>)

Returns a new ObjectId value. The 12-byte ObjectId value consists of

Student and Parent Collection

> db.Parent.insert( { strParentsName: "jon", intAge: 52} )
> db.student.insert( { strName: "jashid", intAge:15, strParentsName : db.Parent.find().strParentsName } )
> db.Parent.find().pretty()Result{
"_id" : ObjectId("5b90f5098618a9e724c017ed"),
"strParentsName" : "Jon",
"intAge" : 52
}

2. UPDATE Value in DataBase

> db.Parent.update( { strParentsName: “jon” },
{ strParentsName: “jon.mon”, intAge: 58})
> db.Parent.update( { strParentsName: “jon.mon” },
{intAge: 58})
> db.Parent.update( { strParentsName: “jon.mon” },
{ strParentsName: “jon hall”, intAge: 55,salary: 5000 },
{ upsert: true })
REsult
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("5b90ff76f7007ba754841d9f")
})

Since upsert is defined as operation that "creates a new document when no document matches the query criteria" there is no place for upsertsin insert command. It is an option for the update command. If you execute command like below it works as an update, if there is a document matching query, or as an insert with document described by update as an argument.

> query ={strParentsName: “YAYA”}
> UPdateData = { strParentsName: “jon hall”, intAge: 55,salary: 5000}
> db.Parent.update(query, UPdateData, {upsert: true})WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("5b90fffef7007ba754841da4")
})
> db.Parent.find().pretty()
{
"_id" : ObjectId("5b90fffef7007ba754841da4"),
"name" : "jon hall",
"intAge" : 55,
"salary" : 5000
}
{
"_id" : ObjectId("5b910058f7007ba754841da9"),
"name" : "YAYA",
"intAge" : 55,
"salary" : 5000
}

3. DELETE Data From DataBase

> db.Parent.deleteOne( { "_id" :ObjectId("5b910058f7007ba754841da9") } );{ "acknowledged" : true, "deletedCount" : 1 }db.Parent.find().pretty(){
"_id" : ObjectId("5b90fffef7007ba754841da4"),
"name" : "jon hall",
"intAge" : 55,
"salary" : 5000
}

All DATA REMOVED

> db.Parent.remove({});WriteResult({ "nRemoved" : 1})> db.Parent.find().pretty()

4.FIND QUERY

>.................................. next day

Jamsheed P

Written by

Co-founder at software Startup https://www.intelliwires.com https://www.appsier.com https://www.worflow.com #Code #Meanstack #node #angular #express #mogodb

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade