model.update在node.js mongoose中不起作用
我希望向我的收藏中添加一个新文档。我正在使用blog.update()进行同样的操作 在此代码中,Blog是一个集合名称,Blog.update不会将博客文章添加到我的数据库中model.update在node.js mongoose中不起作用,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我希望向我的收藏中添加一个新文档。我正在使用blog.update()进行同样的操作 在此代码中,Blog是一个集合名称,Blog.update不会将博客文章添加到我的数据库中 app.post("/compose",function(req,res){ const blogTitle = req.body.postTitle ; const blogPost =req.body.postBody ; Blog.update({},{ $push:
app.post("/compose",function(req,res){
const blogTitle = req.body.postTitle ;
const blogPost =req.body.postBody ;
Blog.update({},{ $push: { title: blogTitle, post: blogPost }, function(error,foundList){
if (error) { console.log(error); }
else { console.log("sucesfully added the item"); console.log(foundList); }
}
});
});
您尝试执行的操作不是Blog.update,它用于更改集合中现有条目中包含的信息;但是Blog.create:
app.post("/compose",function(req,res){
const blogTitle = req.body.postTitle ;
const blogPost =req.body.postBody ;
Blog.create({ title: blogTitle, post: blogPost }, function(error,foundList){
if (error) {
console.log(error);
}
else {
console.log("sucesfully added the item");
console.log(foundList);
}
}
);
我强烈建议您阅读以澄清这些概念。您想更新什么或保存新内容?