Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 通过mongoose进行的所有查询的默认值_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 通过mongoose进行的所有查询的默认值

Node.js 通过mongoose进行的所有查询的默认值,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在尝试编写一个mongoose模式,对于所有对find()或findOne()的调用,该模式将在其一个字段中传递某个值。我尝试在字段声明中使用'default'属性,但这对我不起作用 以下是我的模式: var schema = Schema({ created_at: Date, type: {type: String, default: "alert"}, timestamp: Number, order: Number, description: String,

我正在尝试编写一个mongoose模式,对于所有对find()或findOne()的调用,该模式将在其一个字段中传递某个值。我尝试在字段声明中使用'default'属性,但这对我不起作用

以下是我的模式:

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

});
我希望对find()和findOne()的每次调用都在“type”字段中传递值“alert”


有什么想法吗?

您可以向模型中添加一个简单的包装器方法,该方法将负责查找
类型为:“alert”
的每个文档。大概是这样的:

var Model = mongoose.model('Model', theSchema);
Model.alerts = function (q, callback) {
  q.type = "alert";
  this.find(q, callback);
}
然后,您可以使用
Model.alerts({},callback)
获得您想要的