mongodb Error mongoose do not push object in array

2019-02-24 22:23发布

问题:

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

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

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

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' }

Someone can help me?

回答1:

Try using findOneAndUpdate instead.

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

Hope it helps!



回答2:

If you are using 3.5 MongoDB version or higher, can be an issue with $pushAll, which is deprecated. I founded an option to work around setting usePushEach to true:

new Schema({ arr: [String] }, { usePushEach: true });

Founded in:

https://github.com/Automattic/mongoose/issues/5574#issuecomment-332290518

Can be useful to use the with .push.