MongoDB的常用命令和增查改删
|
数据库操作 |
Mongodb |
MySQL |
|
查询库 |
show databases | show dbs |
show databases |
|
选中库 |
use databaseName |
use databaseName |
|
查询表 |
show tables | show collections |
show tables |
|
创建表 |
db.createCollection(‘collectionName’) |
create table tableName... |
|
删除表 |
db.collectionName.drop |
drop table tableName |
|
删除库 |
db.dropDatabase |
drop database databaseName |
|
添加表数据 |
db.collectionName.insert |
insert |
|
查询表数据 |
db.collectionName.find |
select |
|
修改表数据 |
db.collectionName.update |
update |
|
删除表数据 |
db.collectionName.remove |
delete |
如上述所示,mongodb同样兼容了部分传统数据库的命令,其中有必要说一下的是mongodb中创建库采用的是隐式创建,即在use 一个不存在的库时就会变为创建库,use databaseName 有则选中无则创建,但这里还没有创建完毕,需要进一步创建表才算创建完毕;同时创建表时也允许隐式创建,即db.collectionName.insert 往一个不存在的表中添加数据就会先创建表再添加数据
一:常用命令
1.1:查看当前的数据库
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
shop 0.000GB
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
shop 0.000GB
1.2:选中库
> use shop
switched to db shop
1.3:查看表
> show collections
user
> show tables
user
1.4.1:创建库/表
> use good
switched to db good
> db.createCollection('items')
{ "ok" : 1 }
1.4.2:创建库/表第二种方式
> use cate
switched to db cate
> db.cateList.insert({"_id":1,"cateName":"FAST"})
WriteResult({ "nInserted" : 1 })
1.4.3:在创建库时如果没有创建表,库也会创建失败
> use cate
switched to db cate
> show dbs
admin 0.000GB
config 0.000GB
good 0.000GB
local 0.000GB
shop 0.000GB
1.5:删除表
> db.cateList.drop()
true
1.6:删除库
> use good
switched to db good
> db.dropDatabase()
{ "dropped" : "good", "ok" : 1 }
1.7:使用db.help()查看更多方法
> db.help()
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
db.auth(username, password)
db.cloneDatabase(fromhost) - deprecated
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) - deprecated
db.createCollection(name, {size: ..., capped: ..., max: ...})
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.createUser(userDocument)
db.currentOp() displays currently executing operations in the db
db.dropDatabase()
db.eval() - deprecated
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getLogComponents()
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
db.hostInfo() get details about the server's host
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
db.printSlaveReplicationInfo()
db.dropUser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj: 1}
db.serverStatus()
db.setLogLevel(level,<component>)
db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
db.setVerboseShell(flag) display extra information in shell output
db.shutdownServer()
db.stats()
db.version() current version of the server
二.增:db.collectionName.insert(document);
2.1: 增加单篇文档,如果未指定_id会自动生成:
> use shop
switched to db shop
> db.createCollection('order')
{ "ok" : 1 }
> db.order.insert({"orderCode":"F3K32IR45O","price":150.00})
WriteResult({ "nInserted" : 1 })
> db.order.find()
{ "_id" : ObjectId("5c233308a479af93b5192daa"), "orderCode" : "F3K32IR45O", "price" : 150 }
2.2:增加单个文档并指定_id
> db.order.insert({"_id":2,"orderCode":"F3K32IR45O","price":150.00})
WriteResult({ "nInserted" : 1 })
> db.order.find()
{ "_id" : ObjectId("5c233308a479af93b5192daa"), "orderCode" : "F3K32IR45O", "price" : 150 }
{ "_id" : 2, "orderCode" : "F3K32IR45O", "price" : 150 }
2.3:增加多个文档
> db.order.insert([{"orderCode":"F3K32IR41O","price":1020.00},{"_id":4,"orderCode":"EW3213FW1324","price":11.11}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.order.find()
{ "_id" : ObjectId("5c233308a479af93b5192daa"), "orderCode" : "F3K32IR45O", "price" : 150 }
{ "_id" : 2, "orderCode" : "F3K32IR45O", "price" : 150 }
{ "_id" : ObjectId("5c2334cca479af93b5192dac"), "orderCode" : "F3K32IR41O", "price" : 1020 }
{ "_id" : 4, "orderCode" : "EW3213FW1324", "price" : 11.11 }
2.4:在mongodb3.2版本中新增两个插入方法:
db.collection.insertOne() //将单个文档插入集合中
db.collection.insertMany() //将多个文档插入集合中
三:查:db.collection.find(查询表达式,查询的属性);
3.1:查询所有数据
> db.order.find()
{ "_id" : ObjectId("5c233308a479af93b5192daa"), "orderCode" : "F3K32IR45O", "price" : 150 }
{ "_id" : 2, "orderCode" : "F3K32IR45O", "price" : 150 }
{ "_id" : ObjectId("5c2334cca479af93b5192dac"), "orderCode" : "F3K32IR41O", "price" : 1020 }
{ "_id" : 4, "orderCode" : "EW3213FW1324", "price" : 11.11 }
3.2:查询所有文档的指定属性(_id属性默认查询)
> db.order.find({},{orderCode:1})
{ "_id" : ObjectId("5c233308a479af93b5192daa"), "orderCode" : "F3K32IR45O" }
{ "_id" : 2, "orderCode" : "F3K32IR45O" }
{ "_id" : ObjectId("5c2334cca479af93b5192dac"), "orderCode" : "F3K32IR41O" }
{ "_id" : 4, "orderCode" : "EW3213FW1324" }
3.3:查询所有文档的指定属性且不查询_id
> db.order.find({},{orderCode:1,_id:0}) #属性为1表示查询,为0表示过滤
{ "orderCode" : "F3K32IR45O" }
{ "orderCode" : "F3K32IR45O" }
{ "orderCode" : "F3K32IR41O" }
{ "orderCode" : "EW3213FW1324" }
3.4:查询所有orderCode属性值为F3K32IR45O的文档中的price属性且不查询_id属性
> db.order.find({"orderCode":"F3K32IR45O"},{"price":1,_id:0})
{ "price" : 150 }
{ "price" : 150 }
3.5:比较查询运算符
3.5.1: $gt(大于)
> db.order.find({"price":{$gt:150}},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "F3K32IR41O", "price" : 1020 }
3.5.2:$gte(大于等于)
> db.order.find({"price":{$gte:150}},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "F3K32IR45O", "price" : 150 }
{ "orderCode" : "F3K32IR45O", "price" : 150 }
{ "orderCode" : "F3K32IR41O", "price" : 1020 }
3.5.3:$lt(小于)
> db.order.find({"price":{$lt:150}},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "EW3213FW1324", "price" : 11.11 }
3.5.4:$lte(小于等于)
> db.order.find({"price":{$lte:150}},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "F3K32IR45O", "price" : 150 }
{ "orderCode" : "F3K32IR45O", "price" : 150 }
{ "orderCode" : "EW3213FW1324", "price" : 11.11 }
3.5.5:$eq(等于)
> db.order.find({"price":{$eq:150}},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "F3K32IR45O", "price" : 150 }
{ "orderCode" : "F3K32IR45O", "price" : 150 }
#or
> db.order.find({"price":150},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "F3K32IR45O", "price" : 150 }
{ "orderCode" : "F3K32IR45O", "price" : 150 }
3.5.6:$ne(不等于)
> db.order.find({"price":{$ne:150}},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "F3K32IR41O", "price" : 1020 }
{ "orderCode" : "EW3213FW1324", "price" : 11.11 }
3.5.7:$in(数组中指定的任何值,相当于MySQL的in)
> db.order.find({"price":{$in:[11.11,1020]}},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "F3K32IR41O", "price" : 1020 }
{ "orderCode" : "EW3213FW1324", "price" : 11.11 }
3.5.8:$nin(不在指定的数组中)
> db.order.find({"price":{$nin:[150,1020]}},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "EW3213FW1324", "price" : 11.11 }
3.6:逻辑查询运算符
3.6.1:$and(连接查询子句)
> db.order.find({$and:[{"orderCode":"F3K32IR45O"},{"price":150}]},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "F3K32IR45O", "price" : 150 }
{ "orderCode" : "F3K32IR45O", "price" : 150 }
3.6.2:$not(返回不匹配指定条件的文档)
> db.order.find({"price":{$not:{$gte:150}}},{"orderCode":1,"price":1,"_id":0}) #返回price不大于等于150的文档,也就是返回price小于150的文档
{ "orderCode" : "EW3213FW1324", "price" : 11.11 }
3.6.3:$nor(连接查询子句并返回所有无法匹配指定条件的文档)
> db.order.find({$nor:[{"orderCode":"F3K32IR45O"},{"price":150}]},{"orderCode":1,"price":1,"_id":0}) #返回orderCode不等于F3K32IR45O并且price不等于150的文档
{ "orderCode" : "F3K32IR41O", "price" : 1020 } { "orderCode" : "EW3213FW1324", "price" : 11.11 }
3.6.4:$or(返回匹配任一条件的文档,相当于MySQL的or)
> db.order.find({$or:[{"price":11.11},{"price":1020}]},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "F3K32IR41O", "price" : 1020 }
{ "orderCode" : "EW3213FW1324", "price" : 11.11 }
3.7:元素查询运算符
3.7.1:$exists(匹配具有指定字段的文档)
> db.order.find({"price":{$exists:true}},{"orderCode":1,"price":1,"_id":0}) #$exists为true匹配具有指定字段的文档
{ "orderCode" : "F3K32IR45O", "price" : 150 }
{ "orderCode" : "F3K32IR45O", "price" : 150 }
{ "orderCode" : "F3K32IR41O", "price" : 1020 }
{ "orderCode" : "EW3213FW1324", "price" : 11.11 }
> db.order.find({"price":{$exists:false}},{"orderCode":1,"price":1,"_id":0}) #$exists为false匹配不具有指定字段的文档
3.7.2:$type(匹配指定类型的文档) 可选类型点我
> db.order.find({"orderCode":{$type:2}},{"orderCode":1,"price":1,"_id":0}) #$type可使用数字别名 2表示字符串
{ "orderCode" : "F3K32IR45O", "price" : 150 }
{ "orderCode" : "F3K32IR45O", "price" : 150 }
{ "orderCode" : "F3K32IR41O", "price" : 1020 }
{ "orderCode" : "EW3213FW1324", "price" : 11.11 }
3.8:$regex(正则匹配)
> db.order.find({"orderCode":{$regex:/^E/}},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "EW3213FW1324", "price" : 11.11 }
3.9:findOne(查询单个文档)
> db.order.findOne({"orderCode":"F3K32IR45O"},{"orderCode":1,"price":1,"_id":0})
{ "orderCode" : "F3K32IR45O", "price" : 150 }
四:改:db.user.update(查询表达式,修改的新值,选项{upsert:true/false,multi:true/false})
4.1:$set(设置文档中字段的值,如果设置的字段不存在则创建该字段)
> db.order.update({_id:4},{$set:{"orderCode":"WFIQ13U321UE2","price":111}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.order.find({_id:4})
{ "_id" : 4, "orderCode" : "WFIQ13U321UE2", "price" : 111 }
4.2:$unset(删除指定字段)
> db.order.update({_id:4},{$unset:{"orderCode":""}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
4.3:$inc(按指定的数量增加字段的值)
{ "_id" : 4, "price" : 111, "type" : 1 }
> db.order.update({_id:4},{$inc:{"price":2}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.order.find({_id:4})
{ "_id" : 4, "price" : 113, "type" : 1 }
4.4:$min(指定值小于现有字段值才更新,当指定的字段不存在时变为设置该字段为指定的值)
> db.order.update({_id:4},{$min:{"price":110}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.order.find({_id:4})
{ "_id" : 4, "price" : 110, "type" : 1 }
4.6:$max(指定值大于现有字段值才更新,当指定的字段不存在时变为设置该字段为指定的值)
> db.order.update({_id:4},{$max:{"price":120}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.order.find({_id:4})
{ "_id" : 4, "price" : 120, "type" : 1 }
4.7:$mul(将字段的值乘以指定的值)
> db.order.update({_id:4},{$mul:{"price":2}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.order.find({_id:4})
{ "_id" : 4, "price" : 240, "type" : 1 }
4.8:$rename(重命名字段)
> db.order.update({_id:4},{$rename:{"price":"money"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.order.find({_id:4})
{ "_id" : 4, "type" : 1, "cate" : 0, "money" : 240 }
4.9:在mongodb3.2版本中新增三个修改方法:
db.collection.updateOne() //更新与指定过滤器匹配的单个文档
db.collection.updateMany() //更新与指定过滤器匹配的所有文档
db.collection.replaceOne() //替换与指定过滤器匹配的单个文档
4.10:带可选条件的修改操作(upsert:true/false,multi:true/false)
--upsert 默认为false,无相应记录时是否添加
--multi 默认为false, 是否作用于多条
> db.order.update({},{$set:{"price":2000}},{upsert:true,multi:true})
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })
> db.order.find()
{ "_id" : ObjectId("5c233308a479af93b5192daa"), "price" : 2000 }
{ "_id" : 2, "orderCode" : "F3K32IR45O", "price" : 2000 }
{ "_id" : ObjectId("5c2334cca479af93b5192dac"), "orderCode" : "F3K32IR41O", "price" : 2000 }
{ "_id" : 4, "price" : 2000 }
五:删除: db.collection.remove(查询表达式, 选项{justOne:true/flase})
5.1:删除指定查询条件的所有文档
> db.order.remove({"_id":4})
WriteResult({ "nRemoved" : 1 })
> db.order.find()
{ "_id" : ObjectId("5c233308a479af93b5192daa"), "price" : 2000 }
{ "_id" : 2, "orderCode" : "F3K32IR45O", "price" : 2000 }
{ "_id" : ObjectId("5c2334cca479af93b5192dac"), "orderCode" : "F3K32IR41O", "price" : 2000 }
5.2:删除指定查询条件中所有文档的第一个即指定可选参数justOne:true
> db.order.remove({},true)
WriteResult({ "nRemoved" : 1 })
> db.order.find()
{ "_id" : 2, "price" : 1000 }
{ "_id" : 3, "price" : 1000 }
{ "_id" : 4, "price" : 1000 }
5.3:在mongodb3.2版本中新增两个删除方法:
db.collection.deleteOne() #删除与指定过滤器匹配的单个文档
db.collection.deleteMany() #删除与指定过滤器匹配的所有文档
MongoDB的常用命令和增查改删的更多相关文章
- EF里单个实体的增查改删以及主从表关联数据的各种增删 改查
本文目录 EF对单个实体的增查改删 增加单个实体 查询单个实体 修改单个实体 删除单个实体 EF里主从表关联数据的各种增删改查 增加(增加从表数据.增加主从表数据) 查询(根据主表找从表数据.根据从表 ...
- SQL Server 2012 学习笔记3 增查改删
现在举例几个"增查改删"的语句 select * from UserInfor --查找所有字段 select username,UserId from UserInfor -- ...
- ylb:创建数据库、表,对表的增查改删语句
ylbtech-SQL Server:SQL Server-创建数据库.表,对表的增查改删语句 SQL Server 创建数据库.表,对表的增查改删语句. 1,ylb:创建数据库.表,对表的增查改删语 ...
- DOM树的增查改删总结
DOM树的增查改删总结 摘要:对HTML DOM的操作是前端JavaScript编程时必备的技能,本文是我自己对DOM树操作的总结,主要是方法的罗列,原理性的讲述较少,适合大家用于理清思路或是温习 一 ...
- JS 数组, 对象的增查改删(多语法对比)
数据结构横向对比, 增, 查, 改, 删 建议: 在用数据结构的时候, 优先考虑Map和Set(考虑数据的唯一性), 放弃传统的数组和Object, 特别是比较复杂的数据结构时 数组 Map与Arra ...
- mongodb 增查改删
我们在 MongoDB 之 你得知道MongoDB是个什么鬼 MongoDB - 1 中学习了如果安装部署一个 MongoDB 如果没看到我的金玉良言的话,就重新打开一次客户端和服务端吧 本章我们 ...
- MongoDB入门 常用命令以及增删改查的简单操作
1,运行MongoDB服务mongod --dbpath=/usr/local/developmentTool/mongo/data/db/然后启动客户端mongo2,sudo service mon ...
- MongoDB学习记录(四) - MongoDB的"增查改删"操作之"改"
更新文档主要有以下几种方法: db.collection.updateOne(filter, update, options) db.collection.updateMany(filter, upd ...
- php 连接mongodb 增查改删操作
查询 <?php $m=new MongoClient('mongodb://admin:admin@localhost:27017/admin'); $db=$m->hndb; $cc= ...
随机推荐
- SpringCloud(1)---基于RestTemplate微服务项目案例
基于RestTemplate微服务项目 在写SpringCloud搭建微服务之前,我想先搭建一个不通过springcloud只通过SpringBoot和Mybatis进行模块之间额通讯.然后在此基础上 ...
- Redis学习——数据结构介绍(四)
一.简介 作为一款key-value 的NoSQL数据库,Redis支持的数据结构比较丰富,有:String(字符串) .List(列表) .Set(集合) .Hash(哈希) .Zset(有序集合) ...
- 设计模式的征途—4.抽象工厂(Abstract Factory)模式
上一篇的工厂方法模式引入了工厂等级结构,解决了在原来简单工厂模式中工厂类职责太重的原则,但是由于工厂方法模式的每个工厂只生产一类产品,可能会导致系统中存在大量的工厂类,从而增加系统开销.那么,我们应该 ...
- 命令行分析组件IKende.CLI
IKende.CLI是一款开源的命令行分解组件,它可以简地把命令行字符转换成命令对象.在编写CLI应用的时候经常要对命令字符进行繁锁的分解和转换工作,而IKende.CLI的存也是为了解决以上问题而产 ...
- 不在models.py中的models
概述 如何让你定义的model不在models.py中 在app的models目录中的models 你新建一个app后这个models.py就会自动建立,里面只有几行代码.那么如果是一个中大型项目,每 ...
- linux(centos)搭建.net core 运行环境
总的来说,非常简单,我记录一下: 1.打开https://www.microsoft.com/net/download?initial-os=linux 这里"Instal .NET C ...
- 【Java基础】【07面向对象-构造方法&静态static】
07.01_面向对象(构造方法Constructor概述和格式)(掌握) A:构造方法概述和作用 给对象的数据(属性)进行初始化 B:构造方法格式特点 a:方法名与类名相同(大小也要与类名一致) b: ...
- vnc server的安装
vnc是一款使用广泛的服务器管理软件,可以实现图形化管理.我在安装vnc server碰到一些问题,也整理下我的安装步骤,希望对博友们有一些帮助. 1 安装对应的软件包 [root@centos6 ~ ...
- Jenkins结合.net平台综合之权限修正和文件排除
笔者在发布项目的时候遇到这样一个问题,第一次发布的时候成功发布,然后再次发布失败.但是这个问题很快就排除了,这里提出来是为了帮助遇到这个问题的小伙伴,以顺利避开坑.之所以会这样是因为我们在设置权限的时 ...
- Golang垃圾回收机制(一)
原文: http://legendtkl.com/2017/04/28/golang-gc/ 1. Golang GC 发展 Golang 从第一个版本以来,GC 一直是大家诟病最多的.但是每一个版本 ...