当前位置:网站首页>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
边栏推荐
- Leetcode brush questions: binary tree 11 (balanced binary tree)
- 2022年7月4日-2022年7月10日(ue4视频教程mysql)
- 本季度干货导航 | 2022年Q2
- Reinforcement learning - learning notes 4 | actor critical
- Database logic processing function
- Securerandom things | true and false random numbers
- 怎么挑选好的外盘平台,安全正规的?
- Go language learning tutorial (16)
- Bzoj 3747 poi2015 kinoman segment tree
- C language OJ gets PE, OJ of ACM introduction~
猜你喜欢

2023年深圳市绿色低碳产业扶持计划申报指南

CTF reverse Foundation

leetcode刷题:二叉树11(平衡二叉树)

Enter the parallel world

Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
![信息学奥赛一本通 1339:【例3-4】求后序遍历 | 洛谷 P1827 [USACO3.4] 美国血统 American Heritage](/img/f0/0f985425bd61d9852af0b5fd7307ee.png)
信息学奥赛一本通 1339:【例3-4】求后序遍历 | 洛谷 P1827 [USACO3.4] 美国血统 American Heritage

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

Scala基础【HelloWorld代码解析,变量和标识符】

leetcode刷题:二叉树15(找树左下角的值)

【数字IC验证快速入门】3、数字IC设计全流程介绍
随机推荐
c语言oj得pe,ACM入门之OJ~
Is the education of caiqiantang reliable and safe?
[C language] three implementations of quick sorting and optimization details
【c语言】快速排序的三种实现以及优化细节
【数字IC验证快速入门】3、数字IC设计全流程介绍
Minimum commission for stock trading account opening, where to open an account with low commission? Is it safe to open an account on your mobile phone
Debezium series: PostgreSQL loads the correct last submission LSN from the offset
图嵌入Graph embedding学习笔记
淺淺的談一下ThreadLocalInsecureRandom
解决php无法将string转换为json的办法
Wechat applet regular expression extraction link
E. Singhal and Numbers(质因数分解)
19 Mongoose模块化
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
Debezium series: parsing the default value character set
leetcode刷题:二叉树11(平衡二叉树)
[quick start of Digital IC Verification] 7. Basic knowledge of digital circuits necessary for verification positions (including common interview questions)
1:引文;
Is it safe for Galaxy Securities to open an account online?
Reinforcement learning - learning notes 4 | actor critical