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

电子商务网站设计与实现wordpress教程dara

电子商务网站设计与实现,wordpress教程dara,广州网站建设定制价格,广东外贸网站推广公司目录 一、数据库操作 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.yayakq.cn/news/503124/

相关文章:

  • 内蒙古高等级公路建设开发有限责任公司网站沈阳男科医院在线咨询免费
  • 国外的服务器做的网站在国外能打开在国内打不开是什么原因天猫购物商城
  • 昆明网站搭建多少钱网站怎么样被百度收录
  • 邢台做网站优化费用百度平台
  • 免费用手机制作网站 百度百百斯特网站建设
  • 注册网站会不会有问题信誉好的企业网站建设
  • 网站需要网监备案淄博教育学校网站建设
  • 宁波城乡住房建设局网站西安网站建设有限公司
  • yahoo不收录我的网站广州学做网站
  • 建设的网站百度搜不到域名备案网站
  • 网站开发应用价值wordpress 模版仿米拓
  • 郑州网站建设蝶动企业官网设计
  • 企业网站建设用什么语言从事网站开发需要什么
  • 建设海外网站嘉定企业网站开发建设
  • 昆山网站建设怎么样wordpress换网址
  • 做美容仪器的网站杭州电商网站开发
  • 视频网站如何做引流通过高权重网站做长尾关键词
  • 中卫网站推广营销电商类网站开发定制
  • 响应式网站模板的应用wordpress 分栏
  • 服装公司网站首页廊坊市建设局网站
  • wordpress无法评论某一网站seo策划方案
  • 做网站公司佛山高端网站建设的市场
  • 电子商务网站如何设计网站建设 php
  • 国外有做塑料粒子的网站吗设计旅游网站的主色调
  • 宁波网站优化服务网络营销中的seo与sem
  • 屏蔽网站接口js广告建站公司哪个好
  • 二手商品网站怎么做洛阳网站推广优化
  • 智能建站系统怎么更换网站模板黄石网站设计制作
  • wordpress缓存头像北京谷歌seo
  • 个人备案网站做app建设网站第一部分