首页 > 数据库 >MongoDB 基础(二)mongodb 与 T-SQL 对比

MongoDB 基础(二)mongodb 与 T-SQL 对比

时间:2023-01-27 14:02:27浏览次数:38  
标签:name MongoDB SQL db find tab mongodb id Select


SQL Terms/Concepts

MongoDB Terms/Concepts

database

database

table

Collection(集合)

row

document(文档)

column

Key(键)

Value

Value(值)

index

index

table joins

embedded documents and linking

primary key

primary key

Specify any unique column or column combination as primary key.

In MongoDB, the primary key is automatically set to the _id field.

aggregation (e.g. group by)

aggregation pipeline

 

 

 

 

 

 

 

 

 

 

 

Help

查看帮助(mongodb 中注意大小写)

db.help()

help on db methods

db.mycoll.help()

help on collection methods

sh.help()

sharding helpers

rs.help()

replica set helpers

help admin

administrative help

help connect

connecting to a db help

help keys

key shortcuts

help misc

misc things to know

help mr

mapreduce

show dbs

show database names

show collections

show collections in current database

show users

show users in current database

show profile

show most recent system.profile entries with time >= 1ms

show logs

show the accessible logger names

show log [name]

prints out the last segment of log in memory, 'global' is default

use <db_name>

set current database

db.foo.find()

list objects in collection foo

db.foo.find( { a : 1 } )

list objects in foo where a == 1

it

result of the last line evaluated; use to further iterate

DBQuery.shellBatchSize = x

set default number of items to display on shell

exit

quit the mongo shell

 

 

                                                                                                            数据库

show dbs

查看所有数据库

use mydb

设置为当前数据库

db

查看当前数据库名称

db.createCollection("tab")

db.tab.insert({"id":1})

创建数据库(创建collection)

创建数据库(插入数据也会被创建)

db.copyDatabase("mydb", "newmydb", "127.0.0.1")

复制为另一个新的数据库

db.dropDatabase()

删除数据库

 

 

Collection定义操作(集合必须以字母或下划线开头)

db.createCollection("tab",{size:1024,max:1000,capped:true})

db.tab.insert({"id":1})

创建collections 并做限制(如创建“tab”)

(插入数据也会创建collections)

show collections

查看当前数据库所有collections

db.tab.drop()

删除collections

db.tab.update({},{$rename:{"name":"newname"}},false,true)

更改所有行的字段名name为newname

db.getCollection('tab').renameCollection('newtab')

db.newtab.renameCollection('tab')

更改collection名称

 

 

Collection 数据操作

db.tab.save({"id":2})

db.tab.insert({"id":3})

db.tab.insert([{ size: "S", qty: 25 }, { size: "M", qty: 50 }])

db.tab.insert({ array :[{ size: "S", qty: 25 }, { size: "M", qty: 50 } ]})

插入数据到collections

(会自动生成唯一列 “_id”)

插入多行(相当: insert into tab values(“S”,1), (“M”,50))

字段中插入子集

db.tab.update({"id":1},{$set:{"id":5}})

更改数据(id值由1更改为5)

db.tab.update({ id:2},{id:6, name:'E'})

将id=2的行更新为新的行

db.tab.update({"id":1},{$unset:{ name:'120'}})

删除一个键值对(删除id=2中的字段name)

db.tab.update({"id":1},{$push:{ name:'C'}})

往id=1的行 字段为name数组中插入元素’C’

db.tab.update({"id":1},{$pull:{ name:'C'}})

从id=1的行 字段为name数组中删除所有元素’C’

db.tab.remove({"id":3})

db.tab.remove({"id":3},1)

db.tab.remove({})

删除id=3的所有记录

删除id=3的第一条记录

删除所有记录

 

 

Collection查询数据操作

Select * from tab

db.tab.find()

db.tab.find({})

Select id from tab

db.tab.find({},{"id":1})   #({条件},{字段:0/1})

db.tab.find({},{"_id":0,"id":1})

db.tab.aggregate({ $project : {id : 1 }});

db.tab.find({id:{$exists:1}});

Select * from tab where id=1

db.tab.find( { id :1})

Select id from tab where id=1

db.tab.find({id :1},{"_id":0,"id":1})

Select * from tab where id<>1

db.tab.find({id:{$ne:1}})

Select * from tab where id>2

db.tab.find( { id: { $gt: 2} } )

Select * from tab where id<3

db.tab.find( { id: { $lt: 3} } )

Select * from tab where id>=2

db.tab.find( { id: { $gte: 2 } } )

Select * from tab where id<=3

db.tab.find( { id: { $lte: 3} } )

Select * from tab where id = 1 or id = 2

db.tab.find({$or:[{id:3},{id:2}]})

Select * from tab where id < 2 or id >4

db.tab.find({$or:[{id:{$gt:4}},{id:{$lt:2}}]})

Select * from tab where id in (1,2)

db.tab.find( { id: { $in: [ 1, 2 ] } } )

Select * from tab where id not in (2,3)

db.tab.find({id:{"$nin":[2,3]}})

Select * from tab where id between 2 and 3

Select * from tab where id >= 2 and id <= 3

db.tab.find({$and:[{id:{$gte:2}},{id:{$lte:5}}]})

Select * from tab where id = 1 and name = ‘kk’

db.tab.find({id:2, name:'kk'})

Select distinct id from tab

db.tab.distinct("id");

db.runCommand ( { distinct: "tab", key: "id" } )

Select distinct id from tab where name = ‘A’

db.runCommand({distinct:'tab',key:'id',query:{name:'A'}})

Select * from tab where name like ‘%A%’

db.tab.find({ name:{$regex:"A"}})

db.tab.find({ name:/A/})

Select * from tab order by id asc

db.tab.find().sort({id:1})

Select * from tab order by id desc

db.tab.find().sort({id:-1})

Select  top 5 * from tab

db.tab.find().limit(5)

跳过前m条记

db.tab.find().skip(2)

db.tab.aggregate({ $skip : 2 });

跳过前m条记录,从m+1开始取n条

db.tab.find().skip(2).limit(3)

除了指定的列,其他列都显示

db.tab.find({id:null})

db.tab.find({},{"_id":0})

查找字段id为string类型的行(参考下表格)

db.tab.find({ id: {$type: 2}});

 

 

select name,sum(id) as sumid

from tab

where id >0 group by name

db.tab.group({

key:{ "name":true}  # group by name

,cond:{id:{ $gt:0}}   # where id >0

,reduce:function(obj,prev) #聚合函数

{ prev.sumid += obj.id; } #函数逻辑,累加id

, initial: {sumid: 0 }})  #初始化

Select sum(*) from tab

db.tab.group({key:{},cond:{}

,reduce:function(obj,prev)

{ prev.sumid += obj.id; },initial: {sumid: 0 }});

Select sum(*) as newcol from tab

db.tab.aggregate([{$group:{_id:"$by_user",newcol:{$sum:"$id"}}}])

Select count(*) from tab

db.tab.count()  或者 db.tab.find().count()

Select count(*) from tab

db.tab.group({key:{},cond:{},

reduce:function(obj,prev)

{ prev.sumid += 1; },initial: {sumid: 0 }});

Select avg(*) from tab

db.tab.aggregate([{$group:{_id:"$by_user",newcol:{$avg:"$id"}}}])

Select max(*) from tab

db.tab.find().sort({id:-1}).limit(1)

db.tab.aggregate([{$group:{_id:"$by_user",newcol:{$max:"$id"}}}])

Select min(*) from tab

db.tab.find().sort({id:1}).limit(1)

db.tab.aggregate([{$group:{_id:"$by_user",newcol:{$min:"$id"}}}])

 

 

#元素查询

#db.tab.insert({ratings: [ 5, 8, 9 ] })

db.tab.find({ ratings: [ 5, 8, 9 ] } )   #查找匹配的数组

db.tab.find({ ratings: 5 })     #查找元素中含“5”的记录

db.tab.find({ ratings:{$elemMatch:{$gt:8,$lt:10}}})   #元素匹配查找

#内嵌文档

#db.tab.insert({producer:{company: 'ABC',address: 'Street'}})

#db.tab.insert({producer:[{ company: 'ABC',address: 'Street'},{ company: 'KK',address: 'Street2'}] })

db.tab.find({producer:{company: 'ABC',address: 'Street'}})

db.tab.find({'producer.company': 'ABC'})

db.tab.find( { 'producer.0.address': 'Street'} )  #字段'producer的第一个元素的address=’ Street’

 

 

 

类型描述

类型值

Double

1

String

2

Object

3

Array

4

Binary data

5

Object id

7

Boolean

8

Date

9

Null

10

Regular expression

11

JavaScript code

13

Symbol

14

JavaScript code with scope

15

32-bit integer

16

Timestamp

17

64-bit integer

18

Min key

255

Max key

127

 

 

标签:name,MongoDB,SQL,db,find,tab,mongodb,id,Select
From: https://blog.51cto.com/hzc2012/6024075

相关文章

  • MongoDB 基础(三)mongodb 中的索引使用
    MongoDB中的索引和其他数据库索引类似,也是使用B-Tree结构。MongoDB的索引是在collection级别上的,并且支持在任何列或者集合内的文档的子列中创建索引。 下面是官方给出的一......
  • Windows 2008 双机群集配置(for SQLServer)
    此处配置Windows2008群集,将用于SQLserver2008双机故障转移群集,此处SQLserver的群集基于Windows群集实现。Windows2008集群更容易实现了,只要规划好IP和磁盘分配,可一直......
  • MySQL EXPLAIN 实践汇总
    MySQLEXPLAIN可以显示估计查询语句执行计划,从中可以分析查询的执行情况是不是最优,这有助于我们对不使用索引的查询进行优化。EXPLAIN对于每个查询语句返回一行信息,它列出了......
  • SQLServer 2014 内存优化表
    内存优化表是SQLServer2014的新功能,它是可以将表放在内存中,这会明显提升DML性能。关于内存优化表,更多可参考两位大侠的文章:​​SQLServer2014新特性探秘(1)-内存数据库......
  • 自从学习了MongoDB高可用,慢慢的喜欢上了它,之前确实冷落了
    大家好,我是哪吒,最近项目在使用MongoDB作为图片和文档的存储数据库,为啥不直接存MySQL里,还要搭个MongoDB集群,麻不麻烦?让我们一起,一探究竟,继续学习MongoDB高可用和片键策略,实现......
  • 自从学习了MongoDB高可用,慢慢的喜欢上了它,之前确实冷落了
    大家好,我是哪吒,最近项目在使用MongoDB作为图片和文档的存储数据库,为啥不直接存MySQL里,还要搭个MongoDB集群,麻不麻烦?让我们一起,一探究竟,继续学习MongoDB高可用和片键策略,实......
  • DATETIME和INTERVAL的ANSI SQL标准格式
    TheANSISQLstandardsspecifyqualifiersandformatsforcharacterrepresentationsofDATETIMEandINTERVALvalues.ThestandardqualifierforaDATETIMEval......
  • MySQL基础篇(运算符、排序分页、多表查询、函数)
    MySQL基础篇​​数据库概述​​​​数据库与数据库管理系统​​​​数据库与数据库管理系统的关系​​​​Mysql介绍​​​​RDBMS与非RDBMS​​​​关系型数据库(RDBMS)......
  • 统一观测丨如何使用 Prometheus 监控 MySQL
     MySQL作为最流行的关系型数据库管理系统之一,非常多系统的后端存储都有着MySQL的身影,可谓是广泛应用于各行各业。与此同时,数据库作为应用服务的核心组件,直接影响着应......
  • 统一观测丨如何使用 Prometheus 监控 MySQL
     MySQL作为最流行的关系型数据库管理系统之一,非常多系统的后端存储都有着MySQL的身影,可谓是广泛应用于各行各业。与此同时,数据库作为应用服务的核心组件,直接影响着应......