因为之前有外企两年的工作经验, 而且身边的同事都是外国人, 我从小学二年级学习英语到毕业,突然发现自己学的是哑巴英语,
每次开会,讨论需求和我的美国老大交流时,心里都特别害怕,也听不明白在说什么, 至于尴尬的场面那也是经常出现的, 所以接下来
就有了英孚英语学习的经历;所以接下来我的大部分Blog将要用英语去写了, 希望也是对自己的一个提升; 朋友们要帮我多多监督啊!
What’s Mongoose?
Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment.
Defining a model is as easy as: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 ;
let mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
name: {type: String, default: ''},
id: Number,
password: {type: String, default: ''},
type: {type: Number, default: 0},
group: [],
server_limit: [],
desc: {type: String, default: ''},
nickname: {type: String, default: ''},
age: {type: Number, min: 13, max: 80, default: -1}, // -1 unknown
sex: {type: Number, default: 0},
email: {type: String, default: ''},
id_crad: {type: String, default: ''},
home_address: {type: String, default: ''},
company_address: {type: String, default: ''},
create_time: Date,
update_time: { type: Date, default: Date.now },
headphoto: {type: String, default: ''},
number: {type: String, default: ''},
wechat: {type: String, default: ''},
weibo: {type: String, default: ''},
QQ: {type: String, default: ''},
is_deleted: {type: Number, default: 0}
})
userSchema.index({id: 1});
const User = mongoose.model('User', userSchema);
module.exports = User
Installation
1 | npm install mongoose |
Connecting to MongoDB
config(default.js)
1 | ; |
mongodb(db.js)
1 | ; |
How to start mongodb
1 | const express = require("express"); |
Available Schema Types
- String
- Number
- Date
- Boolean
- Buffer (v2.x only)
- ObjectId(Schema.ObjectId)
- Mixed(Schema.Types.Mixed)
- Array
Additional options
- default: {Function|value}
- required: {Boolean}
- select: {Boolean}
- get: {Function}
- set: {Function}
- index: {Boolean|Object}
- unique: {Boolean}
- sparse: {Boolean}
- validate: {Function|RegExp|Array}
CRUD
C
Model.create(doc(s), [callback])
1 | // save the data of vehicle |
Model#save([options], [options.safe], [options.validateBeforeSave], [fn])
Model.insertMany(doc(s), [options], [callback])
R
Model.find(conditions, [projection], [options], [callback])
conditions: conditions;projection:the field of response;options:options;callback:methed.
1 | VehicleModel.find({ uid: body.uid }, function (err, results) { |
Model.findOne([conditions], [projection], [options], [callback])
Model.findById(id, [projection], [options], [callback])
U
Model.update(conditions, doc, [options], [callback])
1 | let params = { |
Model.updateMany(conditions, doc, [options], [callback])
Model.updateOne(conditions, doc, [options], [callback])
Model.findByIdAndUpdate(id, [update], [options], [callback])
Model.findOneAndUpdate([conditions], [update], [options], [callback])
D
Model.remove(conditions, [callback])
1 | VehicleModel.remove({_id: body._id, uid: body.uid}, function(error, docs){ |
Model.findByIdAndRemove(id, [options], [callback])
Model.findOneAndRemove(conditions, [options], [callback])
More info: