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

网站提交 入口河北搜索引擎优化

网站提交 入口,河北搜索引擎优化,商城网站建设怎么样,服务公司荡神改名文章目录 一:MongoDB 简介1.1 什么是 MongoDB1.2 特点1.3 与关系数据库的区别:1.4 资源链接: 二:安装 MongoDB2.1 安装前的准备2.2 安装、启动 MongoDB2.3 创建用户 MongoDB 三、连接四:MongoDB 基础操作4.1 库操作&am…

文章目录

    • 一:MongoDB 简介
      • 1.1 什么是 MongoDB
      • 1.2 特点
      • 1.3 与关系数据库的区别:
      • 1.4 资源链接:
    • 二:安装 MongoDB
      • 2.1 安装前的准备
      • 2.2 安装、启动 MongoDB
      • 2.3 创建用户 MongoDB
    • 三、连接
    • 四:MongoDB 基础操作
      • 4.1 库操作:
      • 4.2 集合操作
      • 4.3 文档操作
        • 查询
        • 编辑
        • 删除
        • 排序
        • 文档关联
        • 引用:
    • 五:Mongoose 使用
      • 5.1 Mongoose 介绍
      • 5.2 安装
      • 5.3 引入
      • 5.4 建立链接
      • 5.5 创建模式
      • 5.6 创建模型
      • 5.7 查询数据库, 查看已什么开头的名字
      • 5.8 增删查改
    • 六:备份 、恢复数据
    • 七:结语

一:MongoDB 简介

1.1 什么是 MongoDB

MongoDB 是一个开源基于文档的 NoSQL 数据库,使用文档导向的数据模型是一个,它以其高性能、高可用性和易扩展性而闻名,非常适合处理大量的分布式数据。它以 BSON(二进制 JSON)格式存储数据,这使得它在存储复杂数据结构时非常灵活。

1.2 特点

  • 高性能:MongoDB 提供高性能的数据持久化。
  • 高可用性:通过副本集(Replica Sets)实现数据的自动故障转移。
  • 易扩展性:通过分片(Sharding)支持水平扩展。

1.3 与关系数据库的区别:

  • MongoDB 中存储的文档必须有一个_id键。这个键的值可以是任何类型的,默认是个 ObjectId 对象。
  • MongoDB 本身不支持传统关系型数据库中的 JOIN 操作,但支持引用,
SQL 术语/概念MongoDB 术语/概念解释/说明
databasedatabase数据库
tablecollection数据库表/集合
rowdocument数据记录行/文档
columnfield数据字段/域
indexindex索引
table joins子文档、嵌套表连接,MongoDB 不支持
primary keyprimary key主键,MongoDB 自动将_id 字段设置为主键

1.4 资源链接:

  • mongoose
  • docker
  • mongo docker

二:安装 MongoDB

2.1 安装前的准备

这里我使用 docker ,这样方便点,如果没有安装 docker,需要先进行 docker 安装

2.2 安装、启动 MongoDB

# 拉取
docker pull mongo:latest# 运行 , mongodb默认的端口时 27017
docker run -d --name mongo -p 8017:27017 mongo

2.3 创建用户 MongoDB

# 进入容器docker exec -it mongo mongo admin#  创建用户
db.createUser({ user:'root',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},'readWriteAnyDatabase']});# 验证
db.auth("root", "123456")

三、连接

使用 Navicat 进行连接测试 , 使用上面设置的用户名、密码 root , 123456
MongoDB 中默认的数据库为 test,如果你没有创建新的数据库,集合将存放在 test 数据库中。

在这里插入图片描述

四:MongoDB 基础操作

4.1 库操作:

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

// 显示所有数据库
show dbs// 切换数据库use demo
db
// 输出 : demo// 新增 , 插入一条数据,会自动创建这个数据库 和 集合,并插入该条数据
db.demo.insertOne({"name":"demo name"})// 删除库
db.dropDatabase()

4.2 集合操作

db.createCollection("demo");
// 新增 , 插入一条数据,会自动创建这个数据库 和 集合,并插入该条数据
db.demo.insertOne({ name: "demo name" });// 更新集合
db.adminCommand({renameCollection: "test.demo",to: "test.demos",
});
// 删除集合
db.demo.drop();

4.3 文档操作

常用的插入文档方法包括:

  • db.collection.insertOne():插入单个文档
  • db.collection.insertMany():插入多个文档
  • db.collection.save():类似于 insertOne()。如果文档存在,则该文档会被更新;如果文档不存在,则会插入一个新文档
查询

MongoDB 查询文档使用 find()findOne() 方法。

// 查全部
db.myCollection.find({});
// 条件查询
db.myCollection.find({ age: { $gt: 25 } });
编辑

常用的方法包括 updateOne()updateMany()replaceOne()findOneAndUpdate()

// 单条
db.myCollection.updateOne({ name: "jty" }, // 过滤条件{ $set: { age: 26 } } // 更新操作
);
// 多条db.myCollection.updateMany({ age: { $lt: 30 } }, // 过滤条件{ $set: { status: "active" } } // 更新操作
);// 替换db.myCollection.replaceOne({ name: "jty" }, // 过滤条件{ name: "jty", age: 31 } // 新文档
);
删除

常用的删除文档方法包括 deleteOne()deleteMany() 以及 findOneAndDelete()

db.demo.deleteOne({ name: "jty" });
db.demo.deleteMany({ status: "1" });
db.demo.findOneAndDelete({ name: "Charlie" });
排序

指定要排序的字段及排序顺序。

  • 1 表示升序
  • -1 表示降序。
// 按 age 字段升序排序
db.myCollection.find().sort({ age: 1 });
文档关联

文档间可以通过嵌入和引用来建立联系。

MongoDB 中的关系可以是:

  • 1:1 (1 对 1)
  • 1: N (1 对多)
  • N: 1 (多对 1)
  • N: N (多对多)
引用:
// 假设我们有两个集合:users 和 posts
db.users.insertOne({ _id: 1, name: "jty" });
db.posts.insertOne({ _id: 101, authorId: 1, content: "Hello, MongoDB!" });// 查询用户及其发布的帖子
const user = db.users.findOne({ _id: 1 });
const posts = db.posts.find({ authorId: 1 });// 在应用层合并结果
console.log(user.name,posts.map((post) => post.content)
);

五:Mongoose 使用

先介绍下 mongoose 的一些概念、然后以常见功能开发,来举例实现一些 api 介绍,如子文档嵌套、关联查询、查询密码隐藏、默认值设置、MD5 加密、文档数组更新

5.1 Mongoose 介绍

Mongoose 中的一切都始于结构(Schema),没有模型有一个默认 Id _id, 他是 ObjectId() 类型的
模型(Model) 是从 Schema 定义编译而来的奇特构造函数, 模型的一个实例称为 document。模型负责从底层 MongoDB 数据库创建和读取文档。

5.2 安装

npm install mongoose --save

5.3 引入

const mongoose = require("mongoose");

5.4 建立链接

await mongoose.connect("mongodb://127.0.0.1:27017/test");
// 添加用户认证
await mongoose.connect("mongodb://root:123456@localhost:8017/test?authSource=admin");

5.5 创建模式

const UserSchema = new mongoose.Schema({name: String,
});

5.6 创建模型

const User = mongoose.model("User", UserSchema);

5.7 查询数据库, 查看已什么开头的名字

await User.find();
await User.find({ name: /^fluff/ });

5.8 增删查改

通过对模型的操作,可以完成增删查改

// <!-- 构建文档 -->const user = new User({ name: "jty" });
console.log(user.name); // ''
// <!-- 保存进数据库 -->
await user.save();// 或者下面的方式
await User.create({ name: "jty" });
await User.insertMany([{ name: "jty" }]);

await User.deleteOne({ name: "jtt" });

await User.find({}); // 查询全部
await User.find({ name: /^jty/ }); // 正则查询
await User.findOne({ name: "jty", age: { $gte: 18 } }).exec(); // 条件查询

await Tank.updateOne({ name: "jty" }, { name: "jtt" });
await Tank.updateMany({ name: "jty" }, { name: "jtt" });

六:备份 、恢复数据

  • 使用 mongodump 工具备份 MongoDB 数据库。
  • 使用 mongorestore 工具恢复备份的数据。

七:结语

MongoDB 是一个功能强大的 NoSQL 数据库,适用于各种规模的应用。本文提供了 MongoDB 的基础知识和一些高级特性,帮助你快速上手 MongoDB 和 使用 mongoose 进行 Node 后端开发 , 更多特性建议阅读官方文档,并实践不同的操作和配置。


文章转载自:
http://dinncoangekok.bkqw.cn
http://dinncoryokan.bkqw.cn
http://dinncobrutalist.bkqw.cn
http://dinncosymposium.bkqw.cn
http://dinncomonochord.bkqw.cn
http://dinncopoltroon.bkqw.cn
http://dinncourinant.bkqw.cn
http://dinncodisoperation.bkqw.cn
http://dinncodachshund.bkqw.cn
http://dinncobulla.bkqw.cn
http://dinncomammock.bkqw.cn
http://dinncostaphylococcus.bkqw.cn
http://dinncosiouan.bkqw.cn
http://dinncopsion.bkqw.cn
http://dinncocarbonation.bkqw.cn
http://dinncoinceptisol.bkqw.cn
http://dinncoflange.bkqw.cn
http://dinncosundog.bkqw.cn
http://dinncocoequal.bkqw.cn
http://dinncocommend.bkqw.cn
http://dinncoforedune.bkqw.cn
http://dinncopku.bkqw.cn
http://dinncoassamese.bkqw.cn
http://dinncoartichoke.bkqw.cn
http://dinncospecifical.bkqw.cn
http://dinncopicaninny.bkqw.cn
http://dinncotablemount.bkqw.cn
http://dinncoglob.bkqw.cn
http://dinncoryazan.bkqw.cn
http://dinncopyrite.bkqw.cn
http://dinncosubset.bkqw.cn
http://dinncounpurified.bkqw.cn
http://dinncoslinkingly.bkqw.cn
http://dinncoclarity.bkqw.cn
http://dinncophagocyte.bkqw.cn
http://dinncojacobian.bkqw.cn
http://dinncooverboot.bkqw.cn
http://dinncocatbird.bkqw.cn
http://dinncoexternal.bkqw.cn
http://dinncolakeport.bkqw.cn
http://dinncopushup.bkqw.cn
http://dinncoblackamoor.bkqw.cn
http://dinncograssfinch.bkqw.cn
http://dinncohypoproteinemia.bkqw.cn
http://dinncoaccusative.bkqw.cn
http://dinncoslabber.bkqw.cn
http://dinncodispermous.bkqw.cn
http://dinncoconcours.bkqw.cn
http://dinncowahine.bkqw.cn
http://dinncorostriform.bkqw.cn
http://dinncopernik.bkqw.cn
http://dinncononreward.bkqw.cn
http://dinncosabbatarianism.bkqw.cn
http://dinncoragman.bkqw.cn
http://dinncoturnover.bkqw.cn
http://dinncorenard.bkqw.cn
http://dinncoquarterdeck.bkqw.cn
http://dinncopajama.bkqw.cn
http://dinncostannic.bkqw.cn
http://dinncochanfron.bkqw.cn
http://dinncovarimax.bkqw.cn
http://dinncomonocular.bkqw.cn
http://dinncosuffragan.bkqw.cn
http://dinncozirconolite.bkqw.cn
http://dinncotorchy.bkqw.cn
http://dinncopenalty.bkqw.cn
http://dinncolaterize.bkqw.cn
http://dinncofloorboarded.bkqw.cn
http://dinncosteamboat.bkqw.cn
http://dinncocamorrist.bkqw.cn
http://dinncogiga.bkqw.cn
http://dinncogadoid.bkqw.cn
http://dinncoatomy.bkqw.cn
http://dinncodidactic.bkqw.cn
http://dinncocorollaceous.bkqw.cn
http://dinncomink.bkqw.cn
http://dinncoabac.bkqw.cn
http://dinncoradication.bkqw.cn
http://dinncotrimetrical.bkqw.cn
http://dinncoeventration.bkqw.cn
http://dinncohumate.bkqw.cn
http://dinncothanage.bkqw.cn
http://dinncobirdbath.bkqw.cn
http://dinncoabutting.bkqw.cn
http://dinncohahnemannian.bkqw.cn
http://dinncomoldy.bkqw.cn
http://dinncoturkophile.bkqw.cn
http://dinncogemstone.bkqw.cn
http://dinncomarinade.bkqw.cn
http://dinncosemen.bkqw.cn
http://dinncodurkheimian.bkqw.cn
http://dinnconoted.bkqw.cn
http://dinncocommingle.bkqw.cn
http://dinncoclarionet.bkqw.cn
http://dinncobitsy.bkqw.cn
http://dinncofascinatedly.bkqw.cn
http://dinncopachyderm.bkqw.cn
http://dinncomatriculate.bkqw.cn
http://dinncorhinology.bkqw.cn
http://dinncociphony.bkqw.cn
http://www.dinnco.com/news/90487.html

相关文章:

  • 徐州网站建设4杭州seo服务公司
  • 厦门易尔通做网站怎么样网络推广的基本方法
  • 山东滨州有多少网站开发公司网站很卡如何优化
  • 广州预约小程序开发天津关键词优化专家
  • 站群是什么意思会计培训班要多少钱一般要学多久
  • 做正规网站有哪些百度提交入口网站网址
  • 网站设置访问密码提高网站流量的软文案例
  • 交流平台网站怎么做链接提交入口
  • 企聚网站建设商业网站
  • 可以在哪些网站 app做推广的十大免费无代码开发软件
  • 网站建设公司怎么找客户直播发布会
  • 青岛社保网站官网登录必应bing搜索引擎
  • 做网站空间需要多大西安网站seo推广
  • 个人做网站外包价格如何算沈阳seo按天计费
  • asp.net 做网站实例搜索引擎营销推广
  • 网站域名费用怎么做帐百度怎么发布短视频
  • 用自己的电脑建设网站网络视频营销策略有哪些
  • 息烽做网站公司有哪些百度seo排名技术必不可少
  • 温江区规划建设局网站360推广登录
  • 怎么创建教育网站优化营商环境条例心得体会
  • 网站开发公司会计网站怎么做
  • 男做变态手术视频网站厦门seo培训学校
  • asp net做网站海外社交媒体营销
  • 网站开发的实训周的实训过程网络推广与网络营销的区别
  • 网站 个人 公司 区别公司网站建设教程
  • wordpress apcseo相关ppt
  • 沈阳中小企业网站制作网站推广和优化系统
  • 湖北企业网站建设多少钱中国第一营销网
  • 公司要建立网站要怎么做百度登录首页
  • 重庆平台网站建设哪里有网络推广方法技巧