当前位置:网站首页>19 Mongoose模块化
19 Mongoose模块化
2022-07-05 20:11:00 【为什么不好好卖蛋饼】
19 Mongoose模块化
连接数据库,只需要执行一次。
定义一个模块,连接mongodb数据库。
conn_mongo.js
var mongoose=require("mongoose");
mongoose.connect("mongodb://127.0.0.1/mongoose_test",{
userMongoClient:true});
mongoose.connection.once("open",function(){
console.log("数据库连接成功");
});
在新的一个js中调用
require("./tools/conn_mongo");
Model下创建student.js对象
var mongoose=require("mongoose");
var Schema=mongoose.Schema;
var stuSchema=new Schema({
name:String,
age:Number,
});
//定义模型
var StuModel=mongoose.model("student",stuSchema);
//需要将model倒出去
exports.model=StuModel;
require("./tools/conn_mongo");
var Student=require("./model/student").model;
//console.log(Student);
Student.find({
},function (err,docs){
if(!err){
console.log(docs);
}
});
只想暴露模型,其他不想暴露。
require("./tools/conn_mongo");
var Student=require("./model/student");
//console.log(Student);
Student.find({
},function (err,docs){
if(!err){
console.log(docs);
}
});
var mongoose=require("mongoose");
var Schema=mongoose.Schema;
var stuSchema=new Schema({
name:String,
age:Number,
});
//定义模型
var StuModel=mongoose.model("student",stuSchema);
//需要将model倒出去
Module.exports=StuModel;
可以实现代码复用
边栏推荐
- After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
- Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
- What is PyC file
- 基础篇——配置文件解析
- 淺淺的談一下ThreadLocalInsecureRandom
- 【数字IC验证快速入门】8、数字IC中的典型电路及其对应的Verilog描述方法
- Guidelines for application of Shenzhen green and low carbon industry support plan in 2023
- [quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods
- Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
- 浅浅的谈一下ThreadLocalInsecureRandom
猜你喜欢
SecureRandom那些事|真伪随机数
leetcode刷题:二叉树16(路径总和)
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
ACM getting started Day1
Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
【数字IC验证快速入门】1、浅谈数字IC验证,了解专栏内容,明确学习目标
Fundamentals of deep learning convolutional neural network (CNN)
走入并行的世界
A solution to PHP's inability to convert strings into JSON
随机推荐
618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
About the priority of Bram IP reset
Some problems encountered in cocos2d-x project summary
js方法传Long类型id值时会出现精确损失
Build your own website (16)
- Oui. Net Distributed Transaction and Landing Solution
sun. misc. Base64encoder error reporting solution [easy to understand]
y57.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(三十)
[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)
解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
Parler de threadlocal insecurerandom
Zero cloud new UI design
Codeforces Round #804 (Div. 2) - A, B, C
基金网上开户安全吗?去哪里开,可以拿到低佣金?
Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
点云文件的.dat文件读取保存
【数字IC验证快速入门】6、Questasim 快速上手使用(以全加器设计与验证为例)
DP:树DP
计算lnx的一种方式