-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongoose.js
executable file
·152 lines (136 loc) · 3.52 KB
/
mongoose.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var mongoose = require('mongoose'); //引用mongoose模块
var db = mongoose.createConnection('localhost', 'test'); //创建一个数据库连接
db.on('error', console.error.bind(console, '连接错误:'));
db.once('open', function () {
//一次打开记录
});
var PersonSchema = new mongoose.Schema({
name: String //定义一个属性name,类型为String
});
var PersonModel = db.model('Person', PersonSchema);
//如果该Model已经发布,则可以直接通过名字索引到,如下:
//var PersonModel = db.model('Person');
//如果没有发布,上一段代码将会异常
var personEntity = new PersonModel({
name: 'Krouky'
});
//打印这个实体的名字看看
console.log(personEntity.name); //Krouky
//为Schema模型追加speak方法
PersonSchema.methos.speak = function () {
console.log('我的名字叫' + this.name);
}
var PersonModel = db.model('Person', PersonSchema);
var personEntity = new PersonModel({
name: 'Krouky'
});
personEntity.speak(); //我的名字叫Krouky
personEntity.save(); //执行完成后,数据库就有该数据了
PersonModel.find(function (err, persons) {
//查询到的所有person
});
const {
Schema
} = require('mongoose');
const pptDelSchema = new Schema({
live_id: {
type: Schema.Types.ObjectId,
required: true
},
ppt_name: {
type: String
},
ppt_id: {
type: String,
required: true
}
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'update_at'
}
});
pptDelSchema.index({
'ppt_id': 1,
'live_id': 1
});
module.exports = pptDelSchema;
new PptdelModel(newData).save(function (err, item) {
if (err) {
slogger.error('创建PPTDEL失败', err);
return genErrorCallback(
ERROR_CODE.CREATE_PPT_DEL_FAIL,
callback
);
}
console.log('创建PPTDEL成功');
// PptdelModel.find({}, function(err, item) {
// console.log('delppt:', item);
next(null, item);
// })
});
PptModel.remove(conditions, function (err) {
if (err) {
slogger.error('删除PPT失败', err);
return genErrorCallback(
ERROR_CODE.DEL_PPT_FAIL,
callback
);
}
console.log('删除ppt成功');
next(false);
})
LiveModel.findById(data.live_id, 'item._id', function (err, item) {
if (err) {
slogger.error('查询数据库失败', err);
return genErrorCallback(
ERROR_CODE.QUERY_DB_FAIL,
callback
);
}
if (item) {
console.log('live_id存在:', item);
next(null, item);
} else {
return genErrorCallback(
ERROR_CODE.LIVE_ID_NOT_EXIST_IN_DB,
callback
);
}
})
PptModel.findOne({
'live_id': data.live_id,
'ppt_id': data.ppt_id
}, 'item._id', function (err, item) {
if (err) {
slogger.error('查询数据库失败', err);
return genErrorCallback(
ERROR_CODE.QUERY_DB_FAIL,
callback
);
}
if (item) {
return genErrorCallback(
ERROR_CODE.PPT_ADD_REPEATED,
callback
);
} else {
next(null, item);
}
})
LiveModel.update({
_id: data.live_id
}, {
course_name: data.course_name,
start_time: data.begin_time,
end_time: data.end_time
}, function (err, item) {
if (err) {
slogger.error('修改直播数据时失败', err);
return genErrorCallback(
ERROR_CODE.MODIFY_LIVE_FAIL,
callback
);
}
next(false);
});