当前位置:网站首页>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
边栏推荐
- 【数字IC验证快速入门】1、浅谈数字IC验证,了解专栏内容,明确学习目标
- Leetcode(347)——前 K 个高频元素
- Schema和Model
- [quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods
- Cocos2d-x项目总结中的一些遇到的问题
- - Oui. Net Distributed Transaction and Landing Solution
- .Net分布式事务及落地解决方案
- Guidelines for application of Shenzhen green and low carbon industry support plan in 2023
- 死信队列入门(两个消费者,一个生产者)
- 处理文件和目录名
猜你喜欢
Leetcode: binary tree 15 (find the value in the lower left corner of the tree)
Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
【数字IC验证快速入门】1、浅谈数字IC验证,了解专栏内容,明确学习目标
A way to calculate LNX
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
零道云新UI设计中
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
Zero cloud new UI design
Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
Scala基础【HelloWorld代码解析,变量和标识符】
随机推荐
Mysql频繁操作出现锁表问题
js方法传Long类型id值时会出现精确损失
Leetcode brush questions: binary tree 11 (balanced binary tree)
股票开户哪里好?网上客户经理开户安全吗
How to retrieve the root password of MySQL if you forget it
基金网上开户安全吗?去哪里开,可以拿到低佣金?
model方法
js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
Enter the parallel world
sun.misc.BASE64Encoder报错解决方法[通俗易懂]
Schema和Model
《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
leetcode刷题:二叉树11(平衡二叉树)
leetcode刷题:二叉树10(完全二叉树的节点个数)
[quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
1:引文;
微信小程序正则表达式提取链接
ICTCLAS用的字Lucene4.9捆绑
How to choose a good external disk platform, safe and formal?