What is MongoDB?

What is MongoDB?

MongoDB is a document database with the salability and flexibility that you want with the querying and indexing that you need.
MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time
The document model maps to the objects in your application code, making data easy to work with Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze your data MongoDB is a distributed database at its core, so high availability, horizontal scaling, and geographic distribution are built in and easy to use


Basic Useful MongoDB commands –
Download the MongoDB community version?
https://www.mongodb.com/download-center/community

Install MongoDB?

  • –create log folder
  • –create data/db folder
  • –create config file config/mongod.config
  • –create env variable & update path in env variables
  • –follow auth procedure

1) Install:

mongod   --serviceName "MongoDB" --serviceDisplayName "MongoDB 4.0 service" --config "D:MongoDB_4.0configmongod.cfg"   --install
(not needed in the 4.0 as created by default in the bin folder)
Remove the windows service using folllowing command.
mongod   --serviceName "MongoDB" --remove


2) Net Start MongoDB:

Mongodb v3.4 :You need to do the following to create a secure database:
Make sure the user starting the process has permissions and that the directories exist (/data/db in this case).


3) Connect to the instance.

mongo --port 27017


4) Create the user administrator (in the admin authentication database).


use admin
db.createUser(
{
user: "mongoadmin",
pwd: "admin123",
roles: [
{ role: "root", db: "admin" } ] }
)
4) stop
net stop MongoDB


5) start
net start MongoDB

6) Connect and authenticate as the user administrator.
mongo --port 27017 -u "mongoadmin" -p "test" --authenticationDatabase "admin"


5) Re-start the MongoDB instance with access control. (***This is not required****)

***mongod --auth --port 27017 --dbpath "D:MongoDB_4.0datadb"


6) Create additional users as needed for your deployment (in the test authentication database).

use test
db.createUser(
{
user: "localmongo",
pwd: "local1234",
roles: [ { role: "readWrite", db: "test" },
{ role: "read", db: "reports" } ] }
)


7) Connect and authenticate as myTester.


mongo --port 27017 -u "myTester" -p "xyz123" --authenticationDatabase "test"

--start mongodb database
net start MongoDB

--stop mongodb
net stop MongoDB

--go on mongo command prompt
mongo

--connect to database

use MongoSampleDB
db.createUser(
{
user: "mongouser",
pwd: "pass123",
roles: [ { role: "readWrite", db: "MongoSampleDB" }  ] }
)

--create DB USER using only roles and associated DB for that role
use pipelineDB
db.createUser(
{
user: "myTester",
pwd: "xyz123",
roles: [ { role: "readWrite", db: "admin" },
{ role: "read", db: "pipelineDB" } ] }
)

db.createUser(
{
user: "ro-user",
pwd: "test123",
roles: [
{ role: "read", db: "db_replica" } ] }
)

--Re-start the MongoDB instance with access control.
mongod --auth --port 27017 --dbpath  "D:MongoDB_4.0datadb"

--test user/
mongo --port 27017 -u "mongoDBPipelineUser" -p "password" --authenticationDatabase "admin"

--show databases
show dbs

--swicth to mycustomers
use pipelineDB

--mongoDB Create Collection Script
db.createCollection("SampleTable")

--show ccollections
show collections

--insert
db.SampleTable.insert({
"status":"Passed",
"id":"1"

--find
db.SampleTable.find()

--find

db.SampleTable.find()

--find using paramter
db.collection.find( { "id": "15" } )

db.SampleTable.find( { "_id": "4774hfhrb7372" } )

db.SampleTable.find( { "id": "15" } ).forEach(function(record){
print(record.id + "" + record);
});

db.SampleTable.find().forEach(function(user){
print(user.id);
});

--remove
db.SampleTable.remove({ "_id" : "4774hfhrb7372"},1)

--connect using mongo command
mongo -u mongoadmin -p :/

--allow IP address to connect to MongoDB
mongosqld --mongo-uri mongodb://: --mongo-username --mongo-password --mongo-authenticationSource --auth --addr

mongoexport --host=: --db= --collection= -u -p --type csv --out "c:export.csv"  --fields status

Leave a Comment

Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt