MongoDB Basic Use

MongoDB and RDBMS

Mongo RDBMS
Database Database
Collection Table
Document Record

Mongodb

Document
Field and Value pair
Similar JSON object

Install etc.. Pelase see MongoDB Get Started

Mongo Client(command) Use case

Access mongo

mongo

Access database from mongo command

mongo mydb      # mydb is database name

Create new database

use mydb

Create, but if there are no data, we cannot see using show command

Show database list

show dbs

Create collection

db.createCollection("mytable")   # mytable is collection name

Show status

db.stats()

Drop database

After access database

db.dropDatabase()

Rename collection

db.users.renameCollection("member") # users is old name, member is new name

Drop collection

db.mycollection.drop()    # mycollection is collection name

Insert data

We can add different structure object
We can use javascript in mongo shell

db.users.insert(
   {
     name: "dj",
     score: 100
})

db.users.insert(
{
    name: "terawarosu",
	   score: 100,
    tags: ["device", "iphone"]
	})

for(var i=0;i < 10; i++) {
   db.users.insert({
      score: Math.random()
   });
}

Clear all data

db.users.remove({})

Retrieve data(find)

find

db.members.find()   # find all

conditions

db.members.find({score: {$gte: 50}})
db.members.find({team: "A"})
db.members.find({team" {$eq: "K"}})
db.members.find({name: /t/});		// regular expression
db.members.find({name: /^t/});

Other operators, $gte, $gt, $lte, $lt, $eq, $ne

Distinct

Can see what kind of data in "" (candidates?)

db.users.distinct("team")  # A, K, B

Find, where, in

db.members.find({name:/i/, score:{$gte:50}})		// AND
db.members.find({$or: [{name:/i/}, {score:{$gte:50}}])

in

db.members.find({score: {$in: [50,60]}})

Exist column

db.members.find({age: {$exists: true}})
db.members.find({age: {$exists: 0}})

Select column

db.members.find({}, {name: true, score:1})

db.members.find({}, {name: 0, score:1})		# No
db.members.find({}, {name: 0})			# OK
db.members.find({}, {_id: 0, score:1})		# OK  _id is exception

We cannot use both true, false at the same time, but _id is exception

Sort, Limit, Skip

sort

db.members.find({}, {_id: 0}).sort({score: 1})	# ASC
db.members.find({}, {_id: 0}).sort({score: -1})	# DESC

limit

db.members.find({}, {_id: 0}).limit(1)

skip

db.members.find({}, {_id: 0}).skip(2)

Update

db.members.update({name: "tanaka"}, {$set: {score: 80}})

If you set object directly, this is replace

db.members.update({name: "tanaka"}, {name: "tanaka", team: "jungle", point:100})

Update target all

db.members.update({team: "team-1"}, {$set: {score:0}}, {multi: true})

$inc
increate from current status

db.members.update({name: 'muska'}, {$inc: {score: 10}})  # score += 10 we can use minus

$mul
multiple from current status

$rename
rename column

db.members.update({name: 'muska'}, {$rename: {score: "point"}})   // score to point

Update not exist column

db.users.update({name: 'muska'}, {$set: {rank: "taisa"}})

If there are no rank in this document, rank will be inserted
We don't care about document column

Remove field

db.members.update({name: 'muska'}, {$unset: {rank: ""}})  # blank

upsert

db.members.update({name: "ito"}, {name: "ito", score: 99})  // no data, no upate
db.members.update({name: "ito"}, {name: "ito", score: 99}, {upsert: true})  // if not exist, insert data

Index

Index is to ref fast, to add this, reference become faster, but insert become slower to organize index
Check index

db.users.getIndexes()

_id is set by default
Index name is important to identify delete target

create index desc

db.users.createIndex({score: -1})

delete index

db.users.dropIndex("score_-1")

use index name you can see index name with getIndexes

mongodump

mongodump -d mydb   # mydb is database name

Create dump directory under running directory to store dump data

mongorestore

mongorestore --drop

Find dump directory and restore using dump directory data

Ref

ドットインストール
mongodb