当前位置:网站首页>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
边栏推荐
- Wechat applet regular expression extraction link
- ICTCLAS用的字Lucene4.9捆绑
- - Oui. Net Distributed Transaction and Landing Solution
- Debezium series: parsing the default value character set
- Go language | 01 wsl+vscode environment construction pit avoidance Guide
- 14. Users, groups, and permissions (14)
- 点云文件的.dat文件读取保存
- Unity editor extended UI control
- 【c语言】快速排序的三种实现以及优化细节
- Debezium series: PostgreSQL loads the correct last submission LSN from the offset
猜你喜欢
秋招字节面试官问你还有什么问题?其实你已经踩雷了
微信小程序正则表达式提取链接
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
浅浅的谈一下ThreadLocalInsecureRandom
图嵌入Graph embedding学习笔记
[quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
解决php无法将string转换为json的办法
无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
计算lnx的一种方式
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
随机推荐
When JS method passes long type ID value, precision loss will occur
leetcode刷题:二叉树13(相同的树)
怎么挑选好的外盘平台,安全正规的?
股票开户哪里好?网上客户经理开户安全吗
About the priority of Bram IP reset
Codeforces Round #804 (Div. 2) - A, B, C
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
Mysql频繁操作出现锁表问题
Scala basics [HelloWorld code parsing, variables and identifiers]
随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
信息学奥赛一本通 1340:【例3-5】扩展二叉树
Go language | 02 for loop and the use of common functions
Go language | 03 array, pointer, slice usage
期货如何网上开户?安不安全?
无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
中金财富在网上开户安全吗?
sort和投影
Process file and directory names
【数字IC验证快速入门】6、Questasim 快速上手使用(以全加器设计与验证为例)
ROS2专题【01】:win10上安装ROS2