前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MongoDB命令手册

MongoDB命令手册

作者头像
ccf19881030
发布2021-09-08 15:28:23
7130
发布2021-09-08 15:28:23
举报
文章被收录于专栏:ccf19881030的博客

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 } })
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/09/05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • MongoDB命令手册
  • MongoDB命令手册
  • MongoDB Cheat Sheet
    • Show All Databases
      • Show Current Database
        • Create Or Switch Database
          • Drop
            • Create Collection
              • Show Collections
                • Insert Row
                  • Insert Multiple Rows
                    • Get All Rows
                      • Get All Rows Formatted
                        • Find Rows
                          • Sort Rows
                            • Count Rows
                              • Limit Rows
                                • Chaining
                                  • Foreach
                                    • Find One Row
                                      • Find Specific Fields
                                        • Update Row
                                          • Update Specific Field
                                            • Increment Field ($inc)
                                              • Rename Field
                                                • Delete Row
                                                  • Sub-Documents
                                                    • Find By Element in Array ($elemMatch)
                                                      • Add Index
                                                        • Text Search
                                                          • Greater & Less Than
                                                          相关产品与服务
                                                          云数据库 MongoDB
                                                          腾讯云数据库 MongoDB(TencentDB for MongoDB)是腾讯云基于全球广受欢迎的 MongoDB 打造的高性能 NoSQL 数据库,100%完全兼容 MongoDB 协议,支持跨文档事务,提供稳定丰富的监控管理,弹性可扩展、自动容灾,适用于文档型数据库场景,您无需自建灾备体系及控制管理系统。
                                                          领券
                                                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档