当前位置:网站首页>19 mongoose modularization
19 mongoose modularization
2022-07-05 20:17:00 【Why not sell egg cakes well】
19 Mongoose modularization
Connect to database , It only needs to be executed once .
Define a module , Connect mongodb database .
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(" Database connection successful ");
});
In the new one js Call in
require("./tools/conn_mongo");
Model Create student.js object
var mongoose=require("mongoose");
var Schema=mongoose.Schema;
var stuSchema=new Schema({
name:String,
age:Number,
});
// Defining models
var StuModel=mongoose.model("student",stuSchema);
// Need to put model Pour it out
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);
}
});
Just want to expose the model , Others don't want to be exposed .
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,
});
// Defining models
var StuModel=mongoose.model("student",stuSchema);
// Need to put model Pour it out
Module.exports=StuModel;
It can realize code reuse
边栏推荐
- ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development
- DP:树DP
- 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
- [quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
- JVMRandom不可设置种子|问题追溯|源码追溯
- Scala基础【HelloWorld代码解析,变量和标识符】
- sort和投影
- Reinforcement learning - learning notes 4 | actor critical
- 【数字IC验证快速入门】2、通过一个SoC项目实例,了解SoC的架构,初探数字系统设计流程
- [C language] three implementations of quick sorting and optimization details
猜你喜欢
解决php无法将string转换为json的办法
Leetcode brush questions: binary tree 11 (balanced binary tree)
A solution to PHP's inability to convert strings into JSON
.Net分布式事务及落地解决方案
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
Let's talk about threadlocalinsecurerandom
Build your own website (16)
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
ROS2专题【01】:win10上安装ROS2
Wechat applet regular expression extraction link
随机推荐
ROS2专题【01】:win10上安装ROS2
如何形成规范的接口文档
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
Convolution free backbone network: Pyramid transformer to improve the accuracy of target detection / segmentation and other tasks (with source code)
信息学奥赛一本通 1337:【例3-2】单词查找树 | 洛谷 P5755 [NOI2000] 单词查找树
js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
leetcode刷题:二叉树13(相同的树)
Four methods of random number generation | random | math | threadlocalrandom | securityrandom
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
银河证券在网上开户安全吗?
实操演示:产研团队如何高效构建需求工作流?
信息学奥赛一本通 1340:【例3-5】扩展二叉树
Notes on key vocabulary in the English original of the biography of jobs (12) [chapter ten & eleven]
Enter the parallel world
C language OJ gets PE, OJ of ACM introduction~
Debezium series: modify the source code to support drop foreign key if exists FK
【数字IC验证快速入门】7、验证岗位中必备的数字电路基础知识(含常见面试题)
[quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
什么是pyc文件
19 Mongoose模块化