Sunday, July 21, 2019

mongoDB: CRUD operation

Here I tried to setup database, create collection and manipulate document on mongoDB.

Show databases in your environment
> show dbs


Create new database, or switch to existing database
> use articledb


Drop existing database
> use articledb
> db.dropDatabase()


Create collection and insert a document
> db.myNewCollection1.insert( {title:"Super cell appeared in western Japan", body: "People in western region were forced to evacuated blurblur", author:"YangJie", createdOn:"2019/07/20"} )
WriteResult({ "nInserted" : 1 })

> show collections
myNewCollection1


Delete collection
> db.myNewCollection1.drop()
WriteResult({ "nInserted" : 1 })

> show collections



Find inserted document(s) in collection
> db.myNewCollection1.find({})
{ "_id" : ObjectId("5d327a03d192820723ed0f66"), "title" : "Super cell appeared in western Japan", "body" : "People in western region were forced to evacuated blurblur", "author" : "YangJie", "createdOn" : "2019/07/20" }


Find inserted document(s) only with required fields
> fields={"title":1,"author":1,_id:0}
{ "title" : 1, "author" : 1, "_id" : 0 }
> db.myNewCollection1.find({},fields)
{ }
{ "title" : "Super cell appeared in western Japan", "author" : "YangJie" }
> criteria={author:"YangJie"} db.myNewCollection1.find(criteria)


update document(s)
> criteria={author:"YangJie"}
{ "author" : "YangJie" }

> db.myNewCollection1.find(criteria)
{ "_id" : ObjectId("5d33e7fd4d8060b895217fb7"), "title" : "Super cell appeared in western Japan", "body" : "People in western region were forced to evacuated blurblur", "author" : "YangJie", "createdOn" : "2019/07/20" }

> update={$set:{title:"Super cell struck in western Japan region"}}
{ "$set" : { "title" : "Super cell struck in western Japan region" } }

> db.myNewCollection1.update(criteria,update)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.myNewCollection1.find(criteria)
{ "_id" : ObjectId("5d33e7fd4d8060b895217fb7"), "title" : "Super cell struck in western Japan region", "body" : "People in western region were forced to evacuated blurblur", "author" : "YangJie", "createdOn" : "2019/07/20" }


remove specific document(s)
> criteria
{ "author" : "YangJie" }

> db.myNewCollection1.find({})
{ "_id" : ObjectId("5d33e7fd4d8060b895217fb7"), "title" : "Super cell struck in western Japan region", "body" : "People in western region were forced to evacuated blurblur", "author" : "YangJie", "createdOn" : "2019/07/20" }
{ "_id" : ObjectId("5d33e9394d8060b895217fb8"), "title" : "More heavy rain is expected in western Japan", "body" : "People in western region is expected to have torrential rain blurblur,,", "author" : "Denish", "createdOn" : "2019/07/21" }

> db.myNewCollection1.remove(criteria)
WriteResult({ "nRemoved" : 1 })

> db.myNewCollection1.find({})
{ "_id" : ObjectId("5d33e9394d8060b895217fb8"), "title" : "More heavy rain is expected in western Japan", "body" : "People in western region is expected to have torrential rain blurblur,,", "author" : "Denish", "createdOn" : "2019/07/21" }

Wednesday, July 17, 2019

mongoDB: Installing mongoDB on CentOS7

When I tried to install mongoDB by 'yum' command on CentOS7, but it failed due to no entry found on default repository.
So, I created repository file and retry installation.

[root@localhost ~]# vi /etc/yum.repos.d/mongodb-org.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc


[root@localhost ~]# yum install -y mongodb-org



Now the attempt was successful, and I was able to run mongo!

[root@localhost ~]# mongo
MongoDB shell version v3.4.21
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.21
Server has startup warnings:
2019-07-17T22:57:35.959+0900 I CONTROL [initandlisten]
2019-07-17T22:57:35.960+0900 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-17T22:57:35.960+0900 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-07-17T22:57:35.960+0900 I CONTROL [initandlisten]
2019-07-17T22:57:35.960+0900 I CONTROL [initandlisten]
2019-07-17T22:57:35.960+0900 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-07-17T22:57:35.960+0900 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-07-17T22:57:35.961+0900 I CONTROL [initandlisten]
2019-07-17T22:57:35.961+0900 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-07-17T22:57:35.961+0900 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-07-17T22:57:35.961+0900 I CONTROL [initandlisten]