MongoDB命令手册

2021-09-08 15:28:23 浏览数 (1)

MongoDB命令手册

  • MongoDB命令手册
  • MongoDB Cheat Sheet
  • Show All DatabasesShow Current DatabaseCreate Or Switch DatabaseDropCreate CollectionShow CollectionsInsert RowInsert Multiple RowsGet All RowsGet All Rows FormattedFind RowsSort RowsCount RowsLimit RowsChainingForeachFind One RowFind Specific FieldsUpdate RowUpdate Specific FieldIncrement Field (inc)Rename FieldDelete RowSub-DocumentsFind By Element in Array (elemMatch)Add IndexText SearchGreater & Less Than

MongoDB命令手册

MongoDB是一种基于文档的No-SQL非关系型数据库,在Github上面找到一个入门级的MongoDB命令手册教程,地址为:bradtraversy/mongodb_cheat_sheet.md

MongoDB Cheat Sheet

Show All Databases

代码语言:javascript复制
show dbs

Show Current Database

代码语言:javascript复制
db

Create Or Switch Database

代码语言:javascript复制
use acme

Drop

代码语言:javascript复制
db.dropDatabase()

Create Collection

代码语言:javascript复制
db.createCollection('posts')

Show Collections

代码语言:javascript复制
show collections

Insert Row

代码语言:javascript复制
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

代码语言:javascript复制
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

代码语言:javascript复制
db.posts.find()

Get All Rows Formatted

代码语言:javascript复制
db.posts.find().pretty()

Find Rows

代码语言:javascript复制
db.posts.find({ category: 'News' })

Sort Rows

代码语言:javascript复制
# asc
db.posts.find().sort({ title: 1 }).pretty()
# desc
db.posts.find().sort({ title: -1 }).pretty()

Count Rows

代码语言:javascript复制
db.posts.find().count()
db.posts.find({ category: 'news' }).count()

Limit Rows

代码语言:javascript复制
db.posts.find().limit(2).pretty()

Chaining

代码语言:javascript复制
db.posts.find().limit(2).sort({ title: 1 }).pretty()

Foreach

代码语言:javascript复制
db.posts.find().forEach(function(doc) {
  print("Blog Post: "   doc.title)
})

Find One Row

代码语言:javascript复制
db.posts.findOne({ category: 'News' })

Find Specific Fields

代码语言:javascript复制
db.posts.find({ title: 'Post One' }, {
  title: 1,
  author: 1
})

Update Row

代码语言:javascript复制
db.posts.update({ title: 'Post Two' },
{
  title: 'Post Two',
  body: 'New body for post 2',
  date: Date()
},
{
  upsert: true
})

Update Specific Field

代码语言:javascript复制
db.posts.update({ title: 'Post Two' },
{
  $set: {
    body: 'Body for post 2',
    category: 'Technology'
  }
})

Increment Field ($inc)

代码语言:javascript复制
db.posts.update({ title: 'Post Two' },
{
  $inc: {
    likes: 5
  }
})

Rename Field

代码语言:javascript复制
db.posts.update({ title: 'Post Two' },
{
  $rename: {
    likes: 'views'
  }
})

Delete Row

代码语言:javascript复制
db.posts.remove({ title: 'Post Four' })

Sub-Documents

代码语言:javascript复制
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)

代码语言:javascript复制
db.posts.find({
  comments: {
     $elemMatch: {
       user: 'Mary Williams'
       }
    }
  }
)

Add Index

代码语言:javascript复制
db.posts.createIndex({ title: 'text' })

Text Search

代码语言:javascript复制
db.posts.find({
  $text: {
    $search: ""Post O""
    }
})

Greater & Less Than

代码语言:javascript复制
db.posts.find({ views: { $gt: 2 } })
db.posts.find({ views: { $gte: 7 } })
db.posts.find({ views: { $lt: 7 } })
db.posts.find({ views: { $lte: 7 } })

0 人点赞