murvinlai
murvinlai

Reputation: 50345

How to run SlaveOk in Mongoose?

How to call SlaveOK on query in Mongoose?

for example, I have this:

SiteModel.find({}, function(err, docs) { .... } );

Should I do this???

SiteModel.slaveOK().find({}, function(err,docs) { ... } );

Upvotes: 0

Views: 4604

Answers (2)

alexandru.topliceanu
alexandru.topliceanu

Reputation: 2364

mongoose.Query.slaveOk has been deprecated in favour of mongoose.Query.read(readPreference). docs

So, to iterate on the example above:

Model
.where('age').gte(25)
.where('tags').in(['movie', 'music', 'art'])
.select('name', 'age', 'tags')
.skip(20)
.limit(10)
.asc('age')
.read('secondaryPreferred')
.hint({ age: 1, name: 1 })
.run(callback);

Upvotes: 7

alessioalex
alessioalex

Reputation: 63653

Here's the official example from the Mongoose.js website:

Model
.where('age').gte(25)
.where('tags').in(['movie', 'music', 'art'])
.select('name', 'age', 'tags')
.skip(20)
.limit(10)
.asc('age')
.slaveOk()
.hint({ age: 1, name: 1 })
.run(callback);

So I guess your example above would probably work, but do it like so:

Model.find(conditions).slaveOk().run(callback);

Upvotes: 3

Related Questions