【发布时间】:2019-01-03 19:28:34
【问题描述】:
我目前正在使用 mongoose v. 5.25,针对 mongoDB v.3.6。
我的应用程序应该从许多不同的视图中查询数据,例如,我目前在我的数据库中拥有的一个视图:db.joboffers_view.find()
将返回许多从不同集合中聚合的记录。
对于一个普通的集合模型,我这样查询:
const model = db.model(attribute);
/*where attribute, can be any registered schema */
model.find().
then((result) => {
resolve(result);
}).
catch((err) => {
reject(err);
});
然后我注册模型的方式是这样的(简化代码):
//...
//abstracting boring connection methods
const db = mongoose.connection
//...
//simple model schema
const users_schema = {
_id: ObjectId,
another_field: String
};
//here I'm registering a schema for a VIEW, instead of normal collection
const view_schema = {
_id: ObjectId,
another_field: String
};
//...
//then
db.model('users', users_schema);
db.model('view', view_schema);
当我从任何已注册的模型运行查询时,我得到的结果都很好。但是,当我针对代表 mongo 数据库上的视图的模型运行它时,它会返回一个空数组。
没有错误,什么都没有,只是一个空数组。
我查看了 mongoose 文档,但没有找到任何用于查询视图而不是集合数据的特定方法或模式。
这似乎与我对系统中的任何其他集合所做的方式相同。
我错过了什么吗?
【问题讨论】:
标签: mongodb mongoose mongoose-schema