当前位置:网站首页>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;
可以实现代码复用
边栏推荐
- 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
- 【数字IC验证快速入门】3、数字IC设计全流程介绍
- Notes on key vocabulary in the English original of the biography of jobs (12) [chapter ten & eleven]
- ffplay文档[通俗易懂]
- Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
- 点云文件的.dat文件读取保存
- JVMRandom不可设置种子|问题追溯|源码追溯
- [quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods
- Go language learning tutorial (16)
- Leetcode(347)——前 K 个高频元素
猜你喜欢
Build your own website (16)
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
How to select the Block Editor? Impression notes verse, notation, flowus
[quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
Go language | 02 for loop and the use of common functions
Go language | 01 wsl+vscode environment construction pit avoidance Guide
基础篇——配置文件解析
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
leetcode刷题:二叉树10(完全二叉树的节点个数)
随机推荐
Scala basics [HelloWorld code parsing, variables and identifiers]
Zero cloud new UI design
Scala基础【HelloWorld代码解析,变量和标识符】
95后阿里P7晒出工资单:狠补了这个,真香...
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
Analysis of openh264 decoded data flow
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
【数字IC验证快速入门】3、数字IC设计全流程介绍
【数字IC验证快速入门】7、验证岗位中必备的数字电路基础知识(含常见面试题)
Go language learning tutorial (XV)
.Net分布式事務及落地解决方案
Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
How to select the Block Editor? Impression notes verse, notation, flowus
kubernetes资源对象介绍及常用命令(五)-(ConfigMap&Secret)
Parler de threadlocal insecurerandom
[quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods
leetcode刷题:二叉树14(左叶子之和)
怎么挑选好的外盘平台,安全正规的?
走入并行的世界
C - sequential structure