0

I'm trying to write a mongoose schema that for all call to find() or findOne() will pass a certain value in one of its fields. I tried using the 'default' property on the field declaration, but that didn't work for me.

Here's my schema:

var schema = Schema({
  created_at: Date,
  type: {type: String, default: "alert"},
  timestamp: Number,
  order: Number,
  description: String,
  status: String,

});

I would like every call to find() and findOne() to pass the value "alert" in the "type" field.

Any ideas?

1 Answer 1

1

You could add a simple wrapper method to your model which will be responsible for finding every document with type: "alert". Something like this:

var Model = mongoose.model('Model', theSchema);
Model.alerts = function (q, callback) {
  q.type = "alert";
  this.find(q, callback);
}

Then you could get what you want with Model.alerts({}, callback).

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

Comments

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.