inert into users value(3,5)

db.users.insert({a:3,b:5})

 

 

select a,b from users

db.users.find({}, {a:1,b:1})

select * from users

db.users.find()

select * from users where age=33

db.users.find({age:33})

select a,b from users where age=33

db.users.find({age:33}, {a:1,b:1})

select * from users where age=33 order by name

db.users.find({age:33}).sort({name:1})

select * from users where age>33

db.users.find({age:{$gt:33}})

select * from users where age!=33

db.users.find({age:{$ne:33}})

select * from users where name like "%Joe%"

db.users.find({name:/Joe/})

select * from users where name LIKE "Joe%"

db.users.find({name:/^Joe/})

select * from users where age>33 and age<=40

db.users.find({'age':{$gt:33,$lte:40}})

select * from users order by name desc

db.users.find().sort({name:-1})

select * from users where a=1 and b='q'

db.users.find({a:1,b:'q'})

select * from users limit 10 skip 20

db.users.find().limit(10).skip(20)

select * from users where a=1 or b=2

db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )

select * from users limit 1

db.users.findOne()

select order_id from orders o, order_line_items li where li.order_id=o.order_id and li.sku=12345

db.orders.find({"items.sku":12345},{_id:1})

select customer.name from customers,orders where orders.id="q179" and orders.custid=customer.id

var o = db.orders.findOne({_id:"q179"});

var name = db.customers.findOne({_id:o.custid})

 

 

select distinct last_name from users

db.users.distinct('last_name')

select count(*y)

from users

db.users.count()

select count(*y)

from users where age > 30

db.users.find({age: {'$gt': 30}}).count()

select count(age) from users

db.users.find({age: {'$exists': true}}).count()

 

 

create index myindexname on users(name)

db.users.ensureIndex({name:1})

create index myindexname ON users(name,ts desc)

db.users.ensureIndex({name:1,ts:-1})

 

 

explain select * from users where z=3

db.users.find({z:3}).explain()

 

 

update users set a=1 where b='q'

db.users.update({b:'q'}, {$set:{a:1}}, false, true)

update users set a=a+2 where b='q'

db.users.update({b:'q'}, {$inc:{a:2}}, false, true)

 

 

delete from users where z="abc"

db.users.remove({z:'abc'});

更多命令

db.AddUser(username,password)  添加用户

db.auth(usrename,password)     设置数据库连接验证

db.cloneDataBase(fromhost)     从目标服务器克隆一个数据库
db.commandHelp(name)           returns the help for the command
db.copyDatabase(fromdb,todb,fromhost)  复制数据库fromdb---源数据库名称,todb---目标数据库名称,fromhost---源数据库服务器地址
db.createCollection(name,{size:3333,capped:333,max:88888})  创建一个数据集,相当于一个表
db.currentOp()                 取消当前库的当前操作
db.dropDataBase()              删除当前数据库
db.eval(func,args)             run code server-side
db.getCollection(cname)        取得一个数据集合,同用法:db['cname'] or
db.getCollenctionNames()       取得所有数据集合的名称列表
db.getLastError()              返回最后一个错误的提示消息
db.getLastErrorObj()           返回最后一个错误的对象
db.getMongo()                  取得当前服务器的连接对象get the server
db.getMondo().setSlaveOk()     allow this connection to read from then nonmaster membr of a replica pair
db.getName()                   返回当操作数据库的名称
db.getPrevError()              返回上一个错误对象
db.getProfilingLevel()         
db.getReplicationInfo()        获得重复的数据
db.getSisterDB(name)           get the db at the same server as this onew
db.killOp()                    停止(杀死)在当前库的当前操作
db.printCollectionStats()      返回当前库的数据集状态
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus()       返回当前数据库是否为共享数据库
db.removeUser(username)        删除用户
db.repairDatabase()            修复当前数据库
db.resetError()                
db.runCommand(cmdObj)          run a database command. if cmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level)    0=off,1=slow,2=all
db.shutdownServer()            关闭当前服务程序
db.version()                   返回当前程序的版本信息

db.test.find({id:10})          返回test数据集ID=10的数据集
db.test.find({id:10}).count()  返回test数据集ID=10的数据总数
db.test.find({id:10}).limit(2) 返回test数据集ID=10的数据集从第二条开始的数据集
db.test.find({id:10}).skip(8)  返回test数据集ID=10的数据集从0到第八条的数据集
db.test.find({id:10}).limit(2).skip(8)  返回test数据集ID=1=的数据集从第二条到第八条的数据
db.test.find({id:10}).sort()   返回test数据集ID=10的排序数据集
db.test.findOne([query])       返回符合条件的一条数据
db.test.getDB()                返回此数据集所属的数据库名称
db.test.getIndexes()           返回些数据集的索引信息
db.test.group({key:...,initial:...,reduce:...[,cond:...]})
db.test.mapReduce(mayFunction,reduceFunction,<optional params>)
db.test.remove(query)                      在数据集中删除一条数据
db.test.renameCollection(newName)          重命名些数据集名称
db.test.save(obj)                          往数据集中插入一条数据
db.test.stats()                            返回此数据集的状态
db.test.storageSize()                      返回此数据集的存储大小
db.test.totalIndexSize()                   返回此数据集的索引文件大小
db.test.totalSize()                        返回些数据集的总大小
db.test.update(query,object[,upsert_bool]) 在此数据集中更新一条数据
db.test.validate()                         验证此数据集
db.test.getShardVersion()                  返回数据集共享版本号

MongoDB语法与现有关系型数据库SQL语法比较

MongoDB语法                                   MySql语法
db.test.find({'name':'foobar'}) <==> select * from test where name='foobar'
db.test.find()                  <==> select * from test
db.test.find({'ID':10}).count() <==> select count(*) from test where ID=10
db.test.find().skip(10).limit(20)     <==> select * from test limit 10,20
db.test.find({'ID':{$in:[25,35,45]}}) <==> select * from test where ID in (25,35,45)
db.test.find().sort({'ID':-1})        <==> select * from test order by ID desc
db.test.distinct('name',{'ID':{$lt:20}})  <==> select distinct(name) from test where ID<20
db.test.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})  <==> select name,sum(marks) from test group by name
db.test.find('this.ID<20',{name:1})  <==> select name from test where ID<20
db.test.insert({'name':'foobar','age':25})<==>insert into test ('name','age') values('foobar',25)
db.test.remove({})                <==> delete * from test
db.test.remove({'age':20})        <==> delete test where age=20
db.test.remove({'age':{$lt:20}})  <==> elete test where age<20
db.test.remove({'age':{$lte:20}}) <==> delete test where age<=20
db.test.remove({'age':{$gt:20}})  <==> delete test where age>20
db.test.remove({'age':{$gte:20}}) <==> delete test where age>=20
db.test.remove({'age':{$ne:20}})  <==> delete test where age!=20
db.test.update({'name':'foobar'},{$set:{'age':36}}) <==> update test set age=36 where name='foobar'
db.test.update({'name':'foobar'},{$inc:{'age':3}})  <==> update test set age=age+3 where name='foobar'

注意以上命令大小写敏感

mongodb与sql语句对照表的更多相关文章

  1. Mongodb 与 SQL 语句对照表

    In addition to the charts that follow, you might want to consider the Frequently Asked Questions sec ...

  2. MongoDB对应SQL语句

    -------------------MongoDB对应SQL语句------------------- 1.Create and Alter     1.     sql:         crea ...

  3. mongodb 跟踪SQL语句及慢查询收集

    有个需求:跟踪mongodb的SQL语句及慢查询收集 第一步:通过mongodb自带函数可以查看在一段时间内DML语句的运行次数. 在bin目录下面运行  ./mongostat -port 端口号  ...

  4. Mongodb 与sql 语句对照

    此处用mysql中的sql语句做例子,C# 驱动用的是samus,也就是上文中介绍的第一种. 引入项目MongoDB.dll //创建Mongo连接 var mongo = new Mongo(&qu ...

  5. mongodb与sql语句对比

    左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * from users db.users.find({"age" ...

  6. mongodb的sql日志

    在Yii2中是没有打印出mongodb的sql语句,故借用下log来查看吧. 在网上有说可以使用$model->find()->createCommand()->getRawSql( ...

  7. Mongodb操作之查询(循序渐进对比SQL语句)

    工具推荐:Robomongo,可自行百度寻找下载源,个人比较推荐这个工具,相比较mongoVUE则更加灵活. 集合简单查询方法 mongodb语法:db.collection.find()  //co ...

  8. Mongodb操作之查询(循序渐进对比SQL语句)(转http://www.tuicool.com/articles/UzQj6rF)

    工具推荐:Robomongo,可自行百度寻找下载源,个人比较推荐这个工具,相比较mongoVUE则更加灵活. 集合简单查询方法 mongodb语法:db.collection.find()  //co ...

  9. mongodb查询语句与sql语句对比

    左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * from users db.users.find({"age" ...

随机推荐

  1. 为什么QQ浏览器不是默认浏览器但是在打开网页的时候还是默认启动?

    因需要浏览器兼容性测试装了一个QQ浏览器,我将火狐设为默认浏览器,但是每次打开连接的时候都是启动QQ浏览器. 出现这样的情况有以下两种可能:1.取消QQ为默认浏览器打开的设置项未成功,若需要取消QQ为 ...

  2. C标准头文件概述

    C的C89标准一共定义了15个头文件,这些头文件具有幂等性(多次包含同一个头文件的效果等同于只包含了一个头文件,例外),独立性(每个标准头文件的正常工作都不需要以包含其他标准头文件为前提,也没有任何标 ...

  3. [学习嵌入式开发板]iTOP-4412实现NFS网络文件系统

    本文转自迅为:http://www.topeetboard.com 学习平台:iTOP-4412开发板 本文讲解如何在 iTOP-4412 开发板上实现 NFS 网络文件系统. 我们使用的软硬件环境是 ...

  4. Java基本运算符

    1.算术运算符 作用是数字的计算,包括:正号+,负号-,乘*,除/,余%,加+,减-,其算法与数学中的运算相同. 算术运算符实例(假设变量A=10,变量B=20): 操作符 描述 例子 + 正号 +A ...

  5. 使用reportNG替换testNG的默认报告

    关于reportng的官网介绍:http://reportng.uncommons.org/ 1.下载reportNG的jar包:http://pan.baidu.com/s/1hq5znLU 2.r ...

  6. jquery中的clone()方法使用

    clone([Even[,deepEven]]) 描述: 克隆匹配的DOM元素并且选中这些克隆的副本. 在想把DOM文档中元素的副本添加到其他位置时这个函数非常有用. 1:一个布尔值(true 或者 ...

  7. HDU 4777 Rabbit Kingdom --容斥原理+树状数组

    题意: 给一个数的序列,询问一些区间,问区间内与区间其他所有的数都互质的数有多少个. 解法: 直接搞有点难, 所谓正难则反,我们求区间内与其他随便某个数不互质的数有多少个,然后区间长度减去它就是答案了 ...

  8. linux服务器报Too many open files的解决方法

    linux 上tomcat 服务器抛出socket异常“文件打开太多”的问题 java.net.SocketException: Too many open filesat java.net.Plai ...

  9. 分享一个刷网页PV的python小脚本

    下面分享一个小脚本,用来刷网页PV. [root@huanqiu ~]# cat www.py #!/usr/bin/python# coding: UTF-8import webbrowser as ...

  10. iOS中使用RSA对数据进行加密解密

    RSA算法是一种非对称加密算法,常被用于加密数据传输.如果配合上数字摘要算法, 也可以用于文件签名. 本文将讨论如何在iOS中使用RSA传输加密数据. 本文环境 mac os openssl-1.0. ...