找我有兩個模式:貓鼬的對象ID參考
let adSchema = mongoose.Schema (
{
author: {type: ObjectId, ref: 'User'},
title: {type: String, required: true },
town: {type: ObjectId, ref: 'Town', required: true},
}
);
,鎮架構:
let townSchema = mongoose.Schema (
{
name: {type: String, required:true, unique:true},
ads: [{type: ObjectId, ref: 'Ad'}]
}
);
我需要查詢發現通過town.name
我如何能做到這一點的所有廣告?
1)你可以用它做的第一輕鬆查詢上townSchema與鎮名'find'和他們獲得的所有廣告IDS然後adSchema查詢。 2)您可以使用自動填充插件'https:// github.com/mongodb-js/mongoose-autopopulate'。 –