3

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: "[email protected]",
//     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?

2 Answers 2

5

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!

Sign up to request clarification or add additional context in comments.

2 Comments

it works, thanks. However, I wonder that .push does not works in my code.
You can use traditional JavaScript dot notation to update documents in mongoose. However, since you are not using the returned document in your code, findOneAndUpdate is a more appropriate way here. You can read more about updating documents in mongoose in the official documentation: mongoosejs.com/docs/documents
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.

1 Comment

Thanks, I use mongo 3.6.1, and this solve my problem with the old version of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.