貓鼬查詢數據我有一個貓鼬模型,這是非常簡單的:需要填充
aclSchema = mongoose.Schema({
_id: String,
public_read: Boolean,
public_write: Boolean,
});
aclModel = mongoose.model('acl', aclSchema);
引用它另一種模式:
thingSchema = mongoose.Schema({
_id: String,
_acl: { type: String, ref: 'acl' }
});
thingModel = mongoose.model('thing', thingSchema);
我需要能夠找到文檔(thingModel )其中_acl.public_read爲true。我遇到的問題是,因爲thing._acl是一個ref,所以直到查詢完成後才填充它。
例如:
thingModel.find({"_acl.public_read":true}).populate('_acl').exec(callback);
這是因爲,我想不返回任何結果,_acl是一個參考,它不是填充,直到找到返回文件後。
只是一個側面說明,模式比這個更復雜,並且其他的ref可能是循環的。爲了簡單起見,我沒有包括它們,但基本的想法就在那裏。如果真的這麼簡單,我會使用子文件,它會按預期工作。
有沒有更好的方法來做到這一點,所以我得到預期的文件?指定在同一數據庫中收集到執行與連接:
正是我所需要的。任何已知的方式在<3.2? – Alex
@Alex答案已更新。 http://stackoverflow.com/questions/19380738/mongoose-nested-query-on-model-by-field-of-its-referenced-model – sidgate