Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
model.update在node.js mongoose中不起作用_Node.js_Mongodb_Mongoose - Fatal编程技术网

model.update在node.js mongoose中不起作用

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:

我希望向我的收藏中添加一个新文档。我正在使用blog.update()进行同样的操作

在此代码中,Blog是一个集合名称,Blog.update不会将博客文章添加到我的数据库中

  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);
                }
              }
        );

我强烈建议您阅读以澄清这些概念。

您想更新什么或保存新内容?