Contents

Install MongoDB and use commands

1 Environment

  • Mac OS Catalina 10.15.7
1
2
3
4
$ node -v
v14.17.0
$ npm -v
6.14.13

2 Install MongoDB

1
2
$ brew tap mongodb/brew
$ brew install mongodb-community@4.4

Ref. MongoDB document

Warning
$ brew install mongodb is failed as mongodb is removed from brew.

3 Start / Stop MongoDB

3.1 Start

1
2
3
$ brew services start mongodb-community@4.4
...
==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-community)

Then client can access to MongoDB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ mongo
MongoDB shell version v4.4.5
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f44a65e2-9ec7-4ae5-82dd-bb51839f6f2d") }
MongoDB server version: 4.4.5
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
...
> 

3.2 Stop

1
$ brew services stop mongodb-community@4.4

4 database commands

Show test database

1
2
> db
test

Show all database

1
2
3
4
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

Create and switch to new DB.

show dbs does not show the new db before adding data.

1
2
3
4
5
6
7
8
> use news_db
switched to db news_db
> db
news_db
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

Insert data to db

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
> use news_db
switched to db news_db
> db
news_db
> db.article.insert({
... title: "test-title",
... summary: "the first article for test",
... author: "author1",
... published: 2021-05-19,
... content: "test ends up in just a test........"
... })
WriteResult({ "nInserted" : 1 })
> show dbs
admin    0.000GB
config   0.000GB
local    0.000GB
news_db  0.000GB

MongoDB allows to add record with different parameters from prior data like:

1
2
3
4
5
6
7
> db.article.insert({
... title: "test-title2",
... author: "author2",
... distributed: "2021-05-12",
... media: "web",
... content: "test finished"
... })

find()

1
2
3
> db.article.find()
{ "_id" : ObjectId("60a51250c8ad18e364d94d51"), "title" : "test-title", "summary" : "the first article for test", "author" : "author1", "published" : 1997, "content" : "test ends up in just a test........" }
{ "_id" : ObjectId("60a51a6fc8ad18e364d94d52"), "title" : "test-title2", "author" : "author2", "distributed" : "2021-05-12", "media" : "web", "content" : "test finished" }

find() with parameter

1
2
> db.article.find({_id : ObjectId("60a51a6fc8ad18e364d94d52")})
{ "_id" : ObjectId("60a51a6fc8ad18e364d94d52"), "title" : "test-title2", "author" : "author2", "distributed" : "2021-05-12", "media" : "web", "content" : "test finished" }

findOne()

Randomly find one record

1
2
> db.article.findOne()
{ "_id" : ObjectId("60a51250c8ad18e364d94d51"), "published" : "2021-05-19" }

remove()

1
2
3
4
> db.article.remove({_id: ObjectId("60a51250c8ad18e364d94d51")})
WriteResult({ "nRemoved" : 1 })
> db.article.find()
{ "_id" : ObjectId("60a51a6fc8ad18e364d94d52"), "title" : "test-title2", "author" : "author2", "distributed" : "2021-05-12", "media" : "web", "content" : "test finished" }

References