Node.js Mongodb多运算符
有没有办法把这部分写得更好Node.js Mongodb多运算符,node.js,mongodb,express,mlab,Node.js,Mongodb,Express,Mlab,有没有办法把这部分写得更好 'update': function (req, res) { var item = req.body.item; var tags = []; tag.find(function (err, _tags) { tags = _tags; post.update({ '_id': item._id }, { $set: { Text: item.Text } }, function (e1, r1) {
'update': function (req, res) {
var item = req.body.item;
var tags = [];
tag.find(function (err, _tags) {
tags = _tags;
post.update({ '_id': item._id }, { $set: { Text: item.Text } }, function (e1, r1) {
post.update({ '_id': item._id }, { $pullAll: { Tags: tags } }, function (e2, r2) {
post.update({ '_id': item._id }, { $pushAll: { Tags: item.Tags } }, function (e3, r3) {
if (r3 === 1) {
res.json(true);
}
});
});
});
});
}
Post
有一个标签数组,我想更新标签,我得到所有标签,然后$pullAll
将它们清空项目标签数组,然后$pushAll
要再次填充,我不想附加新标签,因为这是一种更新方法,上面的代码按预期工作,但是我猜这有点不正确假设您试图用新的标签集替换现有的标签,您可以将其简化为:
'update': function (req, res) {
var item = req.body.item;
post.update({ '_id': item._id },
{ $set: { Text: item.Text, Tags: item.Tags } },
function (e1, r1) {
if (r1 === 1) {
res.json(true);
}
}
);
}
一个{$set:{Text:item.Text,Tags:item.Tags}}
不会做同样的事情吗?你是对的,回答我会标记,我想我问的方法不正确,你能告诉我如何在一行中生成多个运算符还是我应该生成新问题吗?请参阅其他帖子了解你的后续问题: