我有两个用于与数据库交互的文件。我使用了expressjs,mongoose和body-parser。我的数据库有三个字段:文本,键和状态。我想更新数据库中所有条目的状态。我该如何更新才能达到此目的?
我正在考虑删除所有条目并再次创建所有条目。可能那不是正确的方法
index.js
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var cors = require('cors');
app.use(bodyParser.json());
app.use(cors());
Items = require('./items.js');
mongoose.connect('mongodb://localhost/taskDb');
var db = mongoose.connection;
app.get("/", (req, res) => {
res.send('Visit /api/*****');
});
app.get("/api/items", (req, res) => {
Items.getItems(function (err, items) {
if (err) {
throw err;
}
res.json(items);
});
});
app.post("/api/items", (req, res) => {
var item = req.body;
console.log(item + "item post");
Items.addItem(item, function (err, items) {
if (err) {
throw err;
}
res.json(items);
});
});
app.post("/api/items/complete/", (req, res) => {
var item = req.body;
console.log(item + "item post");
Items.addItem(item, function (err, items) {
if (err) {
throw err;
}
res.json(items);
});
});
app.delete("/api/items/:_key", (req, res) => {
var key =req.params._key;
Items.deleteItem(key, function (err, items) {
if (err) {
throw err;
}
res.json(items);
});
});
app.delete("/api/items/", (req, res) => {
var status = "completed";
Items.deleteItems(status, function (err, items) {
if (err) {
throw err;
}
res.json(items);
});
});
app.put("/api/items/:_key", (req, res) => {
console.log(key);
var key = req.params._key;
var item = req.body;
Items.updateItem(key, item, {}, function (err, items) {
if (err) {
throw err;
}
res.json(items);
});
});
app.listen(3005, () => {
console.log('Listening on port 3005...');
});
items.js
var mongoose = require('mongoose');
var itemSchema = mongoose.Schema({
text: {
type: String,
required: true
},
key: {
type: String,
required: true
},
status: {
type: String,
required: true
}
});
var Item = module.exports = mongoose.model('item', itemSchema);
module.exports.getItems = function (callback, limit) {
Item.find(callback).limit(limit);
}
module.exports.addItem = function (item, callback) {
Item.create(item, callback);
}
module.exports.deleteItem = function (key, callback) {
var query = {key: key};
Item.remove(query, callback);
}
module.exports.deleteItems = function (status, callback) {
var query = {status: status};
Item.remove(query, callback);
}
module.exports.updateItem = function (key, item, options, callback) {
var query = {key: key};
var update = {
text: item.text,
key: item.key,
status: item.status
}
Item.updateOne(query, update, {}, callback);
}
答案 0 :(得分:0)
要更新集合中的所有条目,可以使用 update 功能和选项 multi:true 。
猫鼬文档:Updating Documents
在您的情况下,我测试了3个项目:
我执行了命令:
var mongoose = require('mongoose');
var ItemSchema = new mongoose.Schema({
text: {
type: String,
required: true
},
key: {
type: String,
required: true
},
status: {
type: String,
required: true
}
});
var ItemModel = mongoose.model('Item', ItemSchema);
ItemModel.update({status:'A'},{$set:{key:'update key'}},{multi:true}, function (err, items) {
if (err) {
throw err;
}
console.log(items);
});
在这里您可以看到所有具有 status ='A'的项目已更新为 key:'update key':