Post

MongoDB Cheat Sheet

MongoDB Cheat Sheet

Show All Databases

1
show dbs

Show Current Database

1
db

Create Or Switch Database

1
use acme

Drop

1
db.dropDatabase()

Create Collection

1
db.createCollection('posts')

Show Collections

1
show collections

Insert Row

1
2
3
4
5
6
7
8
9
10
11
db.posts.insert({
  title: 'Post One',
  body: 'Body of post one',
  category: 'News',
  tags: ['news', 'events'],
  user: {
    name: 'John Doe',
    status: 'author'
  },
  date: Date()
})

Insert Multiple Rows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
db.posts.insertMany([
  {
    title: 'Post Two',
    body: 'Body of post two',
    category: 'Technology',
    date: Date()
  },
  {
    title: 'Post Three',
    body: 'Body of post three',
    category: 'News',
    date: Date()
  },
  {
    title: 'Post Four',
    body: 'Body of post three',
    category: 'Entertainment',
    date: Date()
  }
])

Get All Rows

1
db.posts.find()

Get All Rows Formatted

1
db.posts.find().pretty()

Find Rows

1
db.posts.find({ category: 'News' })

Sort Rows

1
2
3
4
# asc
db.posts.find().sort({ title: 1 }).pretty()
# desc
db.posts.find().sort({ title: -1 }).pretty()

Count Rows

1
2
db.posts.find().count()
db.posts.find({ category: 'news' }).count()

Limit Rows

1
db.posts.find().limit(2).pretty()

Chaining

1
db.posts.find().limit(2).sort({ title: 1 }).pretty()

Foreach

1
2
3
db.posts.find().forEach(function(doc) {
  print("Blog Post: " + doc.title)
})

Find One Row

1
db.posts.findOne({ category: 'News' })

Find Specific Fields

1
2
3
4
db.posts.find({ title: 'Post One' }, {
  title: 1,
  author: 1
})

Update Row

1
2
3
4
5
6
7
8
9
db.posts.update({ title: 'Post Two' },
{
  title: 'Post Two',
  body: 'New body for post 2',
  date: Date()
},
{
  upsert: true
})

Update Specific Field

1
2
3
4
5
6
7
db.posts.update({ title: 'Post Two' },
{
  $set: {
    body: 'Body for post 2',
    category: 'Technology'
  }
})

Increment Field ($inc)

1
2
3
4
5
6
db.posts.update({ title: 'Post Two' },
{
  $inc: {
    likes: 5
  }
})

Rename Field

1
2
3
4
5
6
db.posts.update({ title: 'Post Two' },
{
  $rename: {
    likes: 'views'
  }
})

Delete Row

1
db.posts.remove({ title: 'Post Four' })

Sub-Documents

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
db.posts.update({ title: 'Post One' },
{
  $set: {
    comments: [
      {
        body: 'Comment One',
        user: 'Mary Williams',
        date: Date()
      },
      {
        body: 'Comment Two',
        user: 'Harry White',
        date: Date()
      }
    ]
  }
})

Find By Element in Array ($elemMatch)

1
2
3
4
5
6
7
8
db.posts.find({
  comments: {
     $elemMatch: {
       user: 'Mary Williams'
       }
    }
  }
)

Add Index

1
db.posts.createIndex({ title: 'text' })
1
2
3
4
5
db.posts.find({
  $text: {
    $search: "\"Post O\""
    }
})

Greater & Less Than

1
2
3
4
db.posts.find({ views: { $gt: 2 } })
db.posts.find({ views: { $gte: 7 } })
db.posts.find({ views: { $lt: 7 } })
db.posts.find({ views: { $lte: 7 } })
This post is licensed under CC BY 4.0 by the author.