https://cn.mongoosedoc.top/docs/api.html#update_updatehtml
http://www.javashuo.com/article/p-kugkzwjl-hg.htmlnode
var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/studentmange', { autoIndex: false, useNewUrlParser: true }) mongoose.set('useCreateIndex', true); var db = mongoose.connection db.once('open', function(callback) { console.log('數據庫連接成功'); }) module.exports = db
var mongoose = require('mongoose'); var courseSchema = new mongoose.Schema({ "cid": Number, "name": String, "students": [Number] }) courseSchema.index({"cid": 1}) courseSchema.statics.addStudent = function(courses, sid, callback) { console.log('插入課程開始', courses, sid) for(var i=0; i< courses.length; i++) { Course.update({"cid": courses[i]}, {$push: {"student": sid}}, function() { console.log('課程添加報名成功') }) } } var Course = mongoose.model('Course', courseSchema) var course1 = new Course({ "cid": 1, "name": "地理", "students": [] }) course1.save() Course.create({"cid": 2, "name": "數學", "students": [] }) module.exports = Course
exports.add = function(req, res, next) { Student.create(req.query, function() { console.log('插入學生成功') res.send('success') Course.addStudent(req.query.courses, req.query.sid, function() { console.log('插入課程成功'); }) }) } exports.getAll = function(req, res, next) { Student.find({}, function(err, result) { res.send(result) }) } exports.query = function(req, res, next) { Student.findOne({"sid": req.query.sid}, function(err, result) { console.log(result, 'query') res.send(result) }) } exports.edit = function(req, res, next) { console.log('更新 id ', req.query.sid) Student.update({"sid": req.query.sid},{ $set: req.query}, function(err) { res.send('修改爲功') }) } exports.remove = function(req, res, next) { console.log('刪除 id '+ req.query.sid) Student.remove({"sid": req.query.sid}, function(err) { res.send('刪除成功') }) }
$set $lt $gt $push $pullweb
===end 替他具體的細節看文檔吧===mongodb