Mongoose: Turn usePushEach on for all models?

Created on 20 Dec 2017  路  7Comments  路  Source: Automattic/mongoose

Since the mongodb upgraded to 3.6 and dropped all support for $pushAll, Would there be any possible way to set usePushEach on by default for all schemas?

Node 8,
Mongoose 4.13.7
Mongodb 3.6

Most helpful comment

Should be able to do it with a one liner:

mongoose.plugin(schema => { schema.options.usePushEach = true });

We'll be releasing 5.0.0-rc0 soon, which will remove the dependency on $pushAll for good.

All 7 comments

Should be able to do it with a one liner:

mongoose.plugin(schema => { schema.options.usePushEach = true });

We'll be releasing 5.0.0-rc0 soon, which will remove the dependency on $pushAll for good.

mongoose.plugin(schema => { schema.options.usePushEach = true });

where should this go??

@dhazlettjr right after you require mongoose will work.

const mongoose = require('mongoose');
mongoose.plugin(schema => { schema.options.usePushEach = true });
const mySchema = new mongoose.Schema({ stuff: String });
const Model = mongoose.model('model', mySchema);
const otherSchema = new mongoose.Schema({ otherStuff: String });
const OtherModel = mongoose.model('other', otherSchema);

This will apply the plugin to all future schema as documented here

Guys, it worked for me... I have no Idea what is mongoose.plugin(schema => { schema.options.usePushEach = true }); but it works ;-)

Thanks a lot....

Since the mongodb upgraded to 3.6 and dropped all support for $pushAll, Would there be any possible way to set usePushEach with Heroku (using mLab add-on for DB).

I am using Heroku for my application. I am not sure where to provide this.

@ffbeatsolutions676 doesn't matter if you're using heroku or not, the below code should work.

const mongoose = require('mongoose');
mongoose.plugin(schema => { schema.options.usePushEach = true });

Thank you, I've pulling my hair out try to find a solution for this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

lukasz-zak picture lukasz-zak  路  3Comments

adamreisnz picture adamreisnz  路  3Comments

p3x-robot picture p3x-robot  路  3Comments

efkan picture efkan  路  3Comments