mongoose 建立schema 和model
在node中使用MongoDB很多情况下,都是使用mongoose的,所以这集来介绍一下
安装
yarn add mongoose
连接
const mongoose = require("mongoose");
mongoose.connect('mongodb://localhost/my_database');
建立schema 和model
shema 相当于对表结构的定义
model 定义表
其实,在MongoDB中不能称为表,应该为文档
shema
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_database');
const {Schema} = mongoose;
// 用户对象模型
const userSchema = new Schema({
name: {
type: String, //类型
default: Date.now // 默认值
},
avatar: {
type: String,
required: true //必须有值
},
user: String,
passworld: String,
hash: String,
score: Number,
learn: Array,
message: Array,
star: Array,
sign: Array,
signdate: String,
isregister: Boolean,
});
Model
Model是由Schema编译而成的假想(fancy)构造器,具有抽象属性和行为。Model的每一个实例(instance)就是一个document。document可以保存到数据库和对数据库进行操作。
//创建并导出model
const db= {
User: mongoose.model('MUser', muserSchema),
};
module.exports = db;
现在我们就完成了mongodb的数据连接,数据对象模型的创建。
mongoose 建立schema 和model的更多相关文章
- [译]Mongoose指南 - Schema
定义schema 用mongoose的第一件事情就应该是定义schema. schema是什么呢? 它类似于关系数据库的表结构. var mongoose = require('mongoose'); ...
- 85.Mongoose指南 - Schema
转自:https://www.bbsmax.com/A/pRdBnKpPdn/ 定义schema 用mongoose的第一件事情就应该是定义schema. schema是什么呢? 它类似于关系数据库的 ...
- 利用libsvm-mat建立分类模型model参数解密[zz from faruto]
本帖子主要就是讲解利用libsvm-mat工具箱建立分类(回归模型)后,得到的模型model里面参数的意义都是神马?以及如果通过model得到相应模型的表达式,这里主要以分类问题为例子. 测试数据使用 ...
- Ruby学习笔记6: 动态web app的建立(3)--多Model之间的交互
We first built a static site which displayed a static image using only a Controller and a View. This ...
- 【重学Node.js 第3篇】mongodb以及mongoose的使用
mongodb以及mongoose的使用 本篇为这个系列的第三篇,想看更多可以直接去github的项目:https://github.com/hellozhangran/happy-egg-serve ...
- Mongodb学习(1)--- mongoose: Schema, Model, Entity
Schema : 一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力 Model : 由 Schema 发布生成的模型,具有抽象属性和行为的数据库操作 Entity : 由 Model 创建的 ...
- Mongoose:Schema之路
说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. Mongoose学习 这里的Mongoose当然不是图片上的萌物,它是一个MongoDB对象建模工具(object model ...
- Solve Error: MissingSchemaError: Schema hasn't been registered for model "YourModel".
使用MongoDB的时候,如果遇到下面这个错误: /home/ec2-user/YourProject/node_modules/mongoose/lib/index.js: throw new mo ...
- mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)
最后更新时间:2017-07-13 11:10:49 原始文章链接:http://www.lovebxm.com/2017/07/13/mongodb_primer/ MongoDB - 简介 官网: ...
随机推荐
- Twitter Bootstrap:前端框架利器
Bootstrap 的文件结构 读者可以直接从 GitHub 下载到 Bootstrap 源码,本地解压后可以看到这样的目录结构:docs.img.jquery-ui- bootstrap.js 和 ...
- linux(centos)下安装supervisor进程管理工具
在接触supervisor进程管理工具之前,使用springboot打包部署到linux服务器的流程是这样子的,如下图所示: 上图展示的就是最一般的流程,如果项目是小项目或者demo可以这样子去部署, ...
- DataWorks入门
阿里云有很多成熟的云产品(萌新认知),我自己只用过腾讯云的对象存储,对这类云产品不是特别了解. 有幸参与到大数据相关的项目,跟着学了点工具的使用方法,非常简单,也了解了一些使用大数据分析问题的流程. ...
- [Visual Studio] 一些VS2013的使用技巧
作者:h46incon的Blog 1. Peek View 可以在不新建TAB的情况下快速查看.编辑一个函数的代码. 用法:在光标移至某个函数下,按下alt+F12. 然后在Peek窗口里可以继续按a ...
- 一致性Hash算法(转载)
原文地址http://blog.csdn.net/caigen1988/article/details/7708806 consistent hashing 算法早在 1997 年就在论文 Con ...
- investigate issues of real time interrupted
Issues: customer report that real time will interrupted frequently as below: Root Cause: some storm ...
- Python中的猴子补丁
monkey patch指的是在运行时动态替换,一般是在startup的时候.用过gevent就会知道,会在最开头的地方gevent.monkey.patch_all();把标准库中的thread/s ...
- 2018 南京网络预赛Sum - 线性筛
题意 链接 定义 $f(x)$ 为满足以下条件的有序二元组 $(a, b)$ 的方案数(即 $(a, b)$ 与 $(b, a)$ 被认为是不同的方案): $x= ab$ $a$ 和 $b$ 均无平方 ...
- vue 想关工具 及组件
vue-cli vue的脚手架工具 (1) 安装通过 npm install -g vue-cil (2)常用模板 browserify - 拥有高级功能的 Browserify ...
- javascript逻辑非(!/!!)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...