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" }
> 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" }