当前位置:网站首页>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
边栏推荐
- CCPC 2021威海 - G. Shinyruo and KFC(组合数,小技巧)
- [quick start of Digital IC Verification] 7. Basic knowledge of digital circuits necessary for verification positions (including common interview questions)
- Leetcode: binary tree 15 (find the value in the lower left corner of the tree)
- .Net分布式事務及落地解决方案
- [quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
- Leetcode skimming: binary tree 12 (all paths of binary tree)
- Some problems encountered in cocos2d-x project summary
- Go language learning tutorial (XV)
- When JS method passes long type ID value, precision loss will occur
- selenium 元素信息
猜你喜欢
Unity editor extended UI control
计算lnx的一种方式
A solution to PHP's inability to convert strings into JSON
图嵌入Graph embedding学习笔记
. Net distributed transaction and landing solution
【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
95后阿里P7晒出工资单:狠补了这个,真香...
JVMRandom不可设置种子|问题追溯|源码追溯
leetcode刷题:二叉树12(二叉树的所有路径)
Let's talk about threadlocalinsecurerandom
随机推荐
图嵌入Graph embedding学习笔记
B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
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
.Net分布式事务及落地解决方案
leetcode刷题:二叉树14(左叶子之和)
sun. misc. Base64encoder error reporting solution [easy to understand]
ICTCLAS用的字Lucene4.9捆绑
When JS method passes long type ID value, precision loss will occur
Go language | 03 array, pointer, slice usage
Unity编辑器扩展 UI控件篇
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
js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
sort和投影
CCPC 2021威海 - G. Shinyruo and KFC(组合数,小技巧)
c語言oj得pe,ACM入門之OJ~
leetcode刷题:二叉树11(平衡二叉树)
2020 CCPC 威海 - A. Golden Spirit(思维),D. ABC Conjecture(大数分解 / 思维)
A way to calculate LNX
Unity editor extended UI control