Created
July 21, 2017 02:27
-
-
Save JakeLaoyu/0c69f46657a0104b62d297373bf93729 to your computer and use it in GitHub Desktop.
mongoose use
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Student.count({ unit: '图书馆' }, function(err, count) {}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var mongoose = require('mongoose'); | |
| var Schema = mongoose.Schema; | |
| var ObjectId = Schema.Types.ObjectId; | |
| var StudentSchema = new Schema({ | |
| name: String, //姓名 | |
| studentId: Number, //学号 | |
| college: String, //学院 | |
| token: Object, | |
| poor: { | |
| type: Number, | |
| default: 0 | |
| }, //是否贫困 0:不贫困; 1:贫困 | |
| time: [{ | |
| type: ObjectId, | |
| ref: 'Time' | |
| }], | |
| meta: { | |
| createAt: { | |
| type: Date, | |
| default: Date.now() | |
| }, | |
| updateAt: { | |
| type: Date, | |
| default: Date.now() | |
| } | |
| } | |
| }); | |
| StudentSchema.pre('save', function(next) { | |
| if (this.isNew) { | |
| this.meta.createAt = this.meta.updateAt = Date.now(); | |
| } else { | |
| this.meta.updateAt = Date.now(); | |
| } | |
| next(); | |
| }) | |
| module.exports = mongoose.model('Student', StudentSchema); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _student = new Student({ | |
| name: 'Jake', | |
| studentId: '13084233' | |
| }) | |
| _student.save((err,newStudent)=>{ | |
| //do something | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Student | |
| .find({ | |
| name: /Jake/, //正则 | |
| studentId: { $gt: 13084233, $lte: 13084255 }, //学号大于13084233 小于13084255 | |
| college: { | |
| '$in': ['通信工程学院', '会计学院'] | |
| } | |
| }) | |
| .populate('time') | |
| .sort({ studentId: -1, 'meta.createAt': -1 }) // 排序 先按 studentId 排序, studentId 一样再按 meta.createAt 排序 | |
| .skip(3) // 分页,第三页 | |
| .limit(10) // 取十条 | |
| .exec(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Student | |
| .remove({ | |
| _id: { | |
| '$in': [] | |
| } | |
| }) | |
| .exec(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Student.update({ | |
| 'age': 22 | |
| }, { | |
| '$inc': { | |
| 'age': 1 //相当于22+1 | |
| } | |
| }, { | |
| multi: true, //多条 | |
| upsert: true // 如果文档里没有则新建 | |
| }, (err) => { | |
| //do something | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment