mongodb错误mongoose不要在数组$ pushAll中推送对象 [英] mongodb Error mongoose do not push object in array $pushAll

查看:433
本文介绍了mongodb错误mongoose不要在数组$ pushAll中推送对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序,包含User和Post模型,

I have a simple app with User and Post models,

var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/assoc", {useMongoClient:true});
mongoose.Promise = global.Promise;

//Post
var postSchema = new mongoose.Schema({
    title: String,
    content: String
});
var Post = mongoose.model("Post", postSchema);

//User
var userSchema = new mongoose.Schema({
    email: String,
    name: String,
    posts: [postSchema]
});

var User = mongoose.model("User", userSchema);

我之前创建一个用户(名称:gino)并将帖子推送到:

I Create a user before (name: "gino") and push a post into:

// var newUser = new User({
//     email: "a.b@c.it",
//     name: "gino"
// });
//
// newUser.posts.push({
//     title: "gino's post",
//     content: "this is content"
// });
//
// newUser.save(function (err, user) {
//     if (err) {
//         console.log(err);
//     } else {
//         console.log(user);
//     }
// });

还要创建另一个帖子来检查Post模型是否有效:

Also create another post to check if Post model works:

// var newPost = new Post({
//     title: "honky",
//     content: "tonky"
// });
//
// newPost.save(function (err, post) {
//     if (err) {
//         console.log(err);
//     } else {
//         console.log(post);
//     }
// });

当我尝试查找gino并将新项目推入posts数组时出现错误尝试使用此代码段保存用户(user.save):

When I try to find "gino" and push a new item into the posts array I have an error trying to save user (user.save) with this snippet:

User.findOne({name: "gino"}, function (err, user) {
    if (err) {
        console.log(err);
    } else {
        console.log(user);
        user.posts.push({
            title: "post",
            content: "content"
        });
        user.save(function (err, user) {
            if (err) {
                console.log(err);
            } else {
                console.log(user);
            }
        });
    }
});

当我运行应用程序时,我得到了这个:

When I run the app i got this:

{ MongoError: Unknown modifier: $pushAll
    at Function.MongoError.create (appFolder\node_modules\mongodb-core\lib\error.js:31:11)
    at toError (appFolder\node_modules\mongodb\lib\utils.js:139:22)
    at appFolder\node_modules\mongodb\lib\collection.js:1059:67
    at appFolder\node_modules\mongodb-core\lib\connection\pool.js:469:18
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
  name: 'MongoError',
  message: 'Unknown modifier: $pushAll',
  driver: true,
  index: 0,
  code: 9,
  errmsg: 'Unknown modifier: $pushAll' }

有人能帮帮我吗?

推荐答案

请尝试使用 findOneAndUpdate

User.findOneAndUpdate(
  { name: "gino" },
  { $push: { posts: { title: 'post', content: 'content' } } },
  { new: true },
  function (err, user) {
    if(err) console.log("Something wrong when updating data"); 
    console.log(user);  
});

希望有所帮助!

这篇关于mongodb错误mongoose不要在数组$ pushAll中推送对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆