当前位置: 首页 > news >正文

免费个人简历模板上海网络推广优化公司

免费个人简历模板,上海网络推广优化公司,企业自助建站哪家好,广州开发区医院目录 一、数据库操作 1、创建数据库 2、删除数据库 二、集合操作 1、创建集合 2、删除集合 三、文档操作 1、创建文档 2、 插入文档 3、查看文档 4、更新文档 1)update() 方法 2)replace() 方法 一、数据库操作 1、创建数据库 创建数据库…

目录

一、数据库操作

1、创建数据库

2、删除数据库

二、集合操作

1、创建集合

2、删除集合

三、文档操作

1、创建文档

2、 插入文档

3、查看文档

4、更新文档

1)update() 方法

2)replace() 方法


一、数据库操作

1、创建数据库

创建数据库的语法格式如下:

use DATABASE_NAME

如果数据库不存在,则创建数据库,否则切换到该数据库

> show dbs;  admin  0.000GB config 0.000GB  local 0.000GB  mytest 0.000GB  > use test01  
switched to db test01  
​
> db  
test01  
​
> show dbs;  admin  0.000GB config 0.000GB  local 0.000GB  mytest 0.000GB  

数据库创建之后,如果要显示出来,需要插入数据,如下:

test01> db.test01.insert({"name":"zhangsan"});
{acknowledged: true,insertedIds: { '0': ObjectId("64e0919af39cec8c5fc1dec8") }
}
test01> show dbs
admin   40.00 KiB
config  72.00 KiB
local   72.00 KiB
test01  40.00 KiB

MongoDB 中默认的数据库为 test,如果没有创建新的数据库,集合将存放在 test 数据库中。

2、删除数据库

MongoDB 删除数据库的语法格式如下:

  db.dropDatabase()  

删除当前数据库,默认为 test,你可以使用 db 命令查看当前数据库名。

  > dbtest01  > db.dropDatabase()  { "dropped" :  "test01", "ok" : 1 }  > show dbs;  admin  0.000GBconfig 0.000GB  local 0.000GB  mytest 0.000GB  

二、集合操作

1、创建集合

MongoDB 中使用 createCollection() 方法来创建集合。

语法格式:

db.createCollection(name, options)

参数说明:

  • name: 要创建的集合名称

  • options: 可选参数, 指定有关内存大小及索引的选项

    选项可以是以下参数

image-20230201175055270

在插入文档时,MongoDB 首先检查固定集合的 size 字段,然后检查 max 字段:

 #创建集合 test01  > db.createCollection("test01")  { "ok" : 1 }  #查看集合  > show tables;  test01  > show collections  test01  > db.getCollectionNames();  [ "test01" ]  

创建固定集合 mycol,整个集合空间大小 6142800 KB, 文档最大个数为 10000 个。

test> db.createCollection("mytest",{capped: true,autoIndexId:true,size:6142800,max:10000})
{ ok: 1 }test> db.createCollection("mytest01",{capped:  true,size:6142800,max:10000})
{ ok: 1 }
test> show tables;
mytest
mytest01

在  MongoDB 中,不需要创建集合。当插入一些文档时,MongoDB 会自动创建集合。

test01> db.mytest02.insert({"name02":"openlab"})
{acknowledged: true,insertedIds: { '0': ObjectId("64e0939bf39cec8c5fc1dec9") }
}
test01> show tables;
mytest02
test01

2、删除集合

MongoDB 中使用 drop() 方法来删除集合。 语法格式:

  db.collection.drop()  

返回值

如果成功删除选定集合,则 drop() 方法返回 true,否则返回 false。 在数据库 mydb 中,我们可以先通过 show collections 命令查看已存在的集合:

  > db  test  > show tables;  mytest  mytest01  mytest02  > db.mytest.drop()  true  > show tables;  mytest01  mytest02  

三、文档操作

文档的数据结构和 JSON 基本一样。 所有存储在集合中的数据都是 BSON 格式。

BSON 是一种类似 JSON 的二进制形式的存储格式,是 Binary JSON 的简称。

MongoDB 使用 insert() 或 save() 方法向集合中插入文档,语法如下:

 db.collection.insert(document)  db.collection.insertOne()  db.collection.insertMany()  

insert(): 若插入的数据主键已经存在,则会抛 org.springframework.dao.DuplicateKeyException 异常,提示主键重复,不保 存当前数据。

1、创建文档

db.collection.insertOne() 用于向集合插入一个新文档,语法格式如下:

db.collection.insertOne(  <document>,  {  writeConcern: <document>  }  )  

db.collection.insertMany() 用于向集合插入一个多个文档,语法格式如下:

db.collection.insertMany(  [ <document 1>  , <document 2>, ... ],  {  writeConcern: <document>,  ordered: <boolean>  }  )  

参数说明:

  • document:要写入的文档。

  • writeConcern:写入策略,默认为 1,即要求确认写操作,0 是不要求。

  • ordered:指定是否按顺序写入,默认 true,按顺序写入。

    在 test 数据库中创建集合 col,包含如下文档,如果没有该集合,则创建:

#创建集合并插入文档  
test01> use test
switched to db test
test> db.col.insert({title:'MongoDB',
... description:'MongoDB是一个nosql数据库',
... by:'openlab',
... url:'http://www.xiaooupeng.com',
... tags:['mongodb','database','nosql'],
... likes:100
... })
{acknowledged: true,insertedIds: { '0': ObjectId("64e0948ff39cec8c5fc1deca") }
}#查看该集合  
test> db.col.find()
[{_id: ObjectId("64e0948ff39cec8c5fc1deca"),title: 'MongoDB',description: 'MongoDB是一个nosql数据库',by: 'openlab',url: 'http://www.xiaooupeng.com',tags: [ 'mongodb', 'database', 'nosql' ],likes: 100}
]
 先创建变量  
test> document=({title:'MongoDB',
... description:'MongoDB是一个nosql数据库',
... by:'openlab',
... url:'http://www.xiaooupeng.com',
... tags: [ 'mongodb', 'database', 'nosql' ],
... likes: 100
... })执行后输出如下:  
{title: 'MongoDB',description: 'MongoDB是一个nosql数据库',by: 'openlab',url: 'http://www.xiaooupeng.com',tags: [ 'mongodb', 'database', 'nosql' ],likes: 100
}

2、 插入文档

db.collection.insertOne()  # 插入一个文档

db.collection.insertMany() # 插入多个文档

db.collection.insert()       # 插入:可以插入一个文档也可以插入多个文档: 多一个判断的步骤

test> db.co101.insert(document)
{acknowledged: true,insertedIds: { '0': ObjectId("64e0957af39cec8c5fc1decb") }
}

3、查看文档

1)

test> db.co101.find()
[{_id: ObjectId("64e0957af39cec8c5fc1decb"),title: 'MongoDB',description: 'MongoDB是一个nosql数据库',by: 'openlab',url: 'http://www.xiaooupeng.com',tags: [ 'mongodb', 'database', 'nosql' ],likes: 100}
]

2)

test> var document=db.collection.insertOne({"test01":123})
test> document
{acknowledged: true,insertedId: ObjectId("64e095dbf39cec8c5fc1decc")
}

3) 

test> var test02 =  db.coll02.insertMany([{"test02":02},{"test0202":0202}])test> test02
{acknowledged: true,insertedIds: {'0': ObjectId("64e09679f39cec8c5fc1decd"),'1': ObjectId("64e09679f39cec8c5fc1dece")}
}

查询:

db.collection.find()

等值查询:

{field: value}

操作符

{field: {$operater1: value1}.... }

{ status: { $in: [ "A", "D" ] } }

比较

$eq

Matches values that are equal to a specified value.

$gt

Matches values that are greater than a specified value.

$gte

Matches values that are greater than or equal to a specified value.

$in

Matches any of the values specified in an array.

$lt

Matches values that are less than a specified value.

$lte

Matches values that are less than or equal to a specified value.

$ne

Matches all values that are not equal to a specified value.

$nin

Matches none of the values specified in an array.

逻辑

$and

{ $and: [ { status: "A" }, { qty: { $lt: 30 } } ] }

$or

{ $or: [ { status: "A" }, { qty: { $lt: 30 } } ] }

$nor

$not

元素

$exists

$type

# 正则表达式

$regex

{ <field>: { $regex: /pattern/, $options: '<options>' } }

{ <field>: { $regex: 'pattern', $options: '<options>' } }

{ <field>: { $regex: /pattern/<options> } }


4、更新文档

MongoDB 使用 update() 和 replace() 方法来更新集合中的文档。

1)update() 方法

update() 方法用于更新已存在的文档。语法格式如下:

db.collection.update(  <query>,  <update>,  {  upsert: <boolean>,  multi: <boolean>,  writeConcern: <document>  }  )  db.inventory.updateOne({ item: "paper" }, # 查询条件{$set: { "size.uom": "cm", status: "P" }, # $set: 更新操作符# size.uom: cm , status: "P"$currentDate: { lastModified: true }}
)

参数说明:

  • query : update 的查询条件,类似 sql update 查询内 where 后面的。

  • update : update 的对象和一些更新的操作符(如$,$inc...)等,也可以理解为 sql update 查询内 set 后面的

  • upsert : 可选,这个参数的意思是,如果不存在 update 的记录,是否插入 objNew,true 为插入,默认是 false,不插入。

  • multi : 可选,mongodb 默认是 false,只更新找到的第一条记录,如果这个参数 为 true,就把按条件查出来多条记录全部更新。

  • writeConcern :可选,抛出异常的级别。

#查看集合
test> db.co101.find()
[{_id: ObjectId("64e0957af39cec8c5fc1decb"),title: 'MongoDB',description: 'MongoDB是一个nosql数据库',by: 'openlab',url: 'http://www.xiaooupeng.com',tags: [ 'mongodb', 'database', 'nosql' ],likes: 100}
]#更新集合
test> db.co101.update({"title"  : "MongoDB"},{$set:{"title" : "MongoDB_updat  e"}})
{acknowledged: true,insertedId: null,matchedCount: 1,modifiedCount: 1,upsertedCount: 0
}#查看更新后的集合  
#1)
test> db.co101.find()
[{_id: ObjectId("64e0957af39cec8c5fc1decb"),title: 'MongoDB_updat  e',description: 'MongoDB是一个nosql数据库',by: 'openlab',url: 'http://www.xiaooupeng.com',tags: [ 'mongodb', 'database', 'nosql' ],likes: 100}
]
#2)
test> db.co101.find().pretty()
[{_id: ObjectId("64e0957af39cec8c5fc1decb"),title: 'MongoDB_updat  e',description: 'MongoDB是一个nosql数据库',by: 'openlab',url: 'http://www.xiaooupeng.com',tags: [ 'mongodb', 'database', 'nosql' ],likes: 100}
]#以上语句只会修改第一条发现的文档,如果你要修改多条相同的文档,则需要设置  multi 参数为  true
test> db.co101.update({"title" :  "MongoDB"},{$set:{"title" : "MongoDB_updat  e"}},{multi:true})
{acknowledged: true,insertedId: null,matchedCount: 0,modifiedCount: 0,upsertedCount: 0
}

2)replace() 方法

db.inventory.replaceOne({ item: "paper" },{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)
test> db.col01.insertOne(
... {"name":"zhangsan"},
... {"age":22}
... )
{acknowledged: true,insertedId: ObjectId("64e0a0b7f39cec8c5fc1decf")
}test> db.col01.find()
[ { _id: ObjectId("64e0a0b7f39cec8c5fc1decf"), name: 'zhangsan' } ]
test> db.col01.replaceOne({"name":"lisi"},{"age":23})
{acknowledged: true,insertedId: null,matchedCount: 0,modifiedCount: 0,upsertedCount: 0
}test> db.col01.replaceOne({"name":"zhangsan"},{"name":"lisi"})
{acknowledged: true,insertedId: null,matchedCount: 1,modifiedCount: 1,upsertedCount: 0
}
test> db.col01.find()
[ { _id: ObjectId("64e0a0b7f39cec8c5fc1decf"), name: 'lisi' } ]
test>

http://www.tj-hxxt.cn/news/71760.html

相关文章:

  • 网站推广营销公司简述搜索引擎优化
  • 网站开发与java技术app推广平台网站
  • 金华网站制作推广西地那非片
  • 有站点地图的网站百度灰色关键词技术
  • 山东网络推广优化排名优化营商环境发言稿
  • 建设钓鱼网站网站关键词优化代理
  • 深圳品牌网站设计电话网店运营入门基础知识
  • 做网站前端用什么软件好搜索引擎优化管理实验报告
  • 新手学做网站优化关键词优化是怎么弄的
  • 自己家的电脑宽带50m做网站服务器短链接在线生成免费
  • 山东企业展厅设计公司厦门seo关键词优化代运营
  • 美食网站怎么做dw软件开发公司排行榜
  • 苏州做网站品牌公司滨州网站seo
  • 优秀门户网站欣赏百度的网址怎么写
  • 天津做网站好的公司营销技巧培训
  • 网站策划书优势怎么分析软文是什么意思通俗点
  • wordpress只用不带www汕头seo外包公司
  • 网站建设.pdf百度云360建网站
  • 山西网站建设多少钱神马网站快速排名案例
  • 网站建设公司用5g网店运营公司
  • 做网站的的广告词宁波seo推广方式排名
  • 怎么做流量网站优化网站最好的刷排名软件
  • 网站建设公司运营经验b2b十大平台排名
  • 建设网站的企业专业服务今日头条官网
  • 织梦搭建网站教程商丘网站建设公司
  • 用asp做网站怎么布局建站小程序
  • flash做网站通栏百度推广一个月费用
  • 莱芜可信赖的网站建设网络营销八大工具
  • wordpress 文章不显示图片优化一个网站需要多少钱
  • 做水果的网站有哪些chatgpt入口