随笔分类 - MongoDB
摘要:模型 db.js // 连接数据库 const mongoose = require('mongoose') // eggadmin 用户名 // 123456 密码 // 127.0.0.1:27017 服务器及端口 // eggcms 数据库 mongoose.connect('mongodb:
阅读全文
摘要:定义文档结构 分类表 // 连接数据库 const mongoose = require('mongoose') // eggadmin 用户名 // 123456 密码 // 127.0.0.1:27017 服务器及端口 // eggcms 数据库 mongoose.connect('mongod
阅读全文
摘要:定义 Schema const OrderSchema = mongoose.Schema({ order_id: String, uid: Number, trade_no: String, all_price: Number, all_num: Number }) const OrderItem
阅读全文
摘要:内置数据校验方法 required 表示这个数据必须传入 max 用于 Number 类型数据 最大值 min 用于 Number 类型数据 最小值 enum 枚举类型 要求数据必须满足枚举值 match 增加的数据必须符合 match(正则) 的规则 maxlength 最大值 minlength
阅读全文
摘要:添加索引 const UserSchema = mongoose.Schema({ name: { type: String }, sn: { type: String, index: true, // 添加索引 }, age: Number, status: { type: Number, def
阅读全文
摘要:默认参数 const mongoose = require('mongoose') // eggadmin 用户名 // 123456 密码 // 127.0.0.1:27017 服务器及端口 // eggcms 数据库 mongoose.connect('mongodb://eggadmin:12
阅读全文
摘要:安装 npm install mongoose --save 配置 const mongoose = require('mongoose') // eggadmin 用户名 // 123456 密码 // 127.0.0.1:27017 服务器及端口 // eggcms 数据库 mongoose.c
阅读全文
摘要:管道操作符 $project 增加、删除、重命名字段 (可以筛选指定的字段出现) $match 条件匹配,只有满足条件的文档才能进入下一个阶段 $limit 限制结果的数量 $skip 跳过文档的数量 $sort 条件排序 $group 条件组合结果 $lookup 可以引入其他集合的数据 插入数据
阅读全文
摘要:创建超级管理员账户 use admin db.createUser({ user: "admin", pwd: "123456", roles: [ {role: "root", db: "admin"} ] }) 修改 Mongodb 数据库配置文件 MongoDB/Server/5.0/bin/
阅读全文
摘要:下载 https://www.mongodb.com/try/download/database-tools 添加到Mongo的bin目录中 导入,将 data.txt 文件导入到数据库的文档中 {"id": "10001", "name": "小明", "age": "18", "sex": "男
阅读全文
摘要:文档游标 执行 find 方法后该方法的返回值称为文档游标 let arr = []; for(let i = 0; i < 100; i++) { arr.push({name: 'js' + i, age: 18 + i}) } db.person.insertMany(arr); MongoD
阅读全文
摘要:find db.<collection>.find( <query>, <projection> ); query:查询条件 projection:投影文档,规定了查询的结果中显示哪些字段 查询所有文档 db.person.find() 查询 person 集合中所有的文档(不传入条件默认查询所有文
阅读全文
摘要:insertMany db.<collection>.insertMany( [<document>, ...], { writeConcern: <document>, ordered: <boolean> } ); ordered 取值默认为true 代表按顺序写入,如果写入文档出错,则后面所有
阅读全文
摘要:insertOne db.<collection>.insertOne( <document>, { writeConcern: <document> } ) document 需要写入的文档 writeConcern 写入安全级别 往 person 集合中写入一个文档 db.person.inse
阅读全文
摘要:主键 MongoDB的主键用于保证每条数据的唯一性 每一个文档被添加到集合之后,MongoDB会自动添加主键 主键的名称是 _id 默认情况下文档的主键是 ObjectId 类型的数据 ObjectId 类型是12个字节的字符串 ObjectId("61e96767a033bdae9c1b2a07"
阅读全文
摘要:连接MongoDB服务器 mongo 命令连接数据库 查看所有数据库 show dbs 数据库基本操作 use demo 创建一个名称叫 demo 的数据库 db 进入刚才创建的数据库 show collections 查看当前数据库中的集合 db.createCollection('person'
阅读全文
摘要:下载 下载地址 https://www.mongodb.com/try/download/ 选择社区免费版本 mongoDB Community Server
阅读全文