MongoDb基本增删改查操做总结

前言

以前一直习惯用Mysql数据库,换了MongoDb的命令行老是不熟练,这里作一个MongoDb命令行的总结,之后能方便查阅。正则表达式


MongoDb基本使用

1. 数据库操做

查看数据库sql

show dbs 复制代码

统计数据库信息数据库

use test  # 切换到test数据库

db.stats()         //统计数据信息
{ 
    "db" : "test",     //数据库名
    "collections" : 0, //集合数量
    "views" : 0,
    "objects" : 0,     //文档数量
    "avgObjSize" : 0,  //平均每一个文档的大小
    "dataSize" : 0,    //数据占用空间大小,不包括索引,单位为字节
    "storageSize" : 0, //分配的存储空间
    "nuinExtents" : 0, //连续分配的数据块
    "indexes" : 0,     //索引个数
    "indexsize" : 0,   //索引占用空间大小
    "fileSize" : 0,    //物理存储文件的大小
    "ok" : 1 
}
复制代码

删除数据库bash

db.dropDatabase ()    //删除当前数据库,db指向当前使用的test数据库复制代码

查看该数据库下的集合app

db.getCollectionNames()复制代码


2. 集合操做

建立集合ui

db.createCollection(name, options)

eg:db.createCollection("yingSet", {capped:true,size:6142800, max :10000 }) #建立yingSet数据库复制代码
                                                options 能够使用的选项
参数 类型 描述
capped Boolean (可选)若是为 true,则启用封闭的集合。上限集合是固定大小的集合,它在达到其最大时自动覆盖其最旧的条目。若是指定 true,则还须要指定 size 参数
size 数字 (可选)指定上限集合的最大大小(以字节为单位)。若是 capped 为 true,那么还须要指定次字段的值
max 数字 (可选)指定上限集合中容许的最大文档数

若是向一个没有建立的集合中插入文档,那么会先建立这个集合spa

db.yingSet.insert( {"name": "tom"} )    # db.yingSet指向集合对象复制代码


查看该数据库下的集合命令行

show collections
cms_config
cms_page
cms_site
cms_site_server
cms_template
filesystem
fs.chunks
fs.files
sys_dictionary
user_test
复制代码

重命名集合code

db.yingSet.renameCollection( "myset")复制代码

删除集合server

db.yingSet.drop()复制代码


3. 文档操做

插入操做

插入不指定 _id 字段的文档

db.test.insert( { item : "card", qty : 15 })  #向test集合插入数据复制代码

插入指定 _id 字段的文档,值 _id 必须在集合中惟一,以免重复键错误

db.test.insert(
    { _id: 10, item: "box", qty: 20 }
) 

复制代码

用变量方式插入文档

do = ({ name: "c语言", price: 40 }) 
db.test.insert(do)复制代码


MongoDB 3.2 更新后新增

db.test.insertOne( { item: "card", qty: 15 } );复制代码

插入的多个文档

db.test.insertMany([
    { item: "card", qty: 15 },
    { item: "envelope", qty: 20 },
    { item: "stamps", qty:30 }
])复制代码


更新修改操做

obj 表明须要更新的对象,若是集合内部已经存在一个与 obj 相同的“_id”的记录,Mongodb 会把 obj 对象替换为集合内已存在的记录;若是不存在,则会插入 obj 对象。

db.collection.save ( obj )

eg:
db.products.save( { _id: 100, item: "watern, qty: 30 })复制代码


删除操做

db.test.remove({'title': 'MongoDB'})复制代码

db.collection.deleteMany ({})  
db.collection.deleteMany ({ status : "A" })
db.collection.delete.One ({ status : "D" })复制代码

查询操做

基本条件查询

db.test.find()
db.test.find().pretty()

复制代码
                                 MongoDB 与 RDBMS 的查询比较
操做符 格式 实例 与 RDBMS where 语句比较
等于(=) {<key> : {<value>}} db.test.find( {price : 24} ) where price = 24
大于(>) {<key> : {$gt : <value>}} db.test.find( {price : {$gt : 24}} ) where price > 24
小于(<) {<key> : {$lt : <value>}} db.test.find( {price : {$lt : 24}} ) where price < 24
大于等于(>=) {<key> : {$gte : <value>}} db.test.find( {price : {$gte : 24}} ) where price >= 24
小于等于(<=) {<key> : {$lte : <value>}} db.test.find( {price : {$lte : 24}} ) where price <= 24
不等于(!=) {<key> : {$ne : <value>}} db.test.find( {price : {$ne : 24}} ) where price != 24
与(and) {key01 : value01, key02 : value02, ...} db.test.find( {name : "《》", price : 24} ) where name = "《》" and price = 24
或(or) {$or : [{key01 : value01}, {key02 : value02}, ...]} db.test.find( {$or:[{name : "《》"},{price : 24}]} ) where name = "《》" or price = 24

查询 age 为 null 的字段

db.test.find({age:null})复制代码

限制查询结果的个数

db.test.find().limit(3)复制代码

用于对查询结果进行排序,1 是升序,-1 是降序

db.test.find().sort({"price" : 1})复制代码

使用 $regex 操做符来设置匹配字符串的正则表达式

db.test.find({tags:{$regex:"MongoDB"}})复制代码
相关文章
相关标签/搜索