[英]How to search on mongoose with exclude
我有 2 個架構,如下所示,我需要從 videoInfos 中獲取 50 個項目,而 userInfos.watched 中沒有這個項目(這個數組內容是 videoInfos 的內容 _id)。 如果可以,請使用像 videoInfos.find().exce 這樣的語法。
const userSchema = new mongoose.Schema({
username:String,
password:String,
point: Number,
watched: Array,//content _id of videoInfos
running: Array,
});
const userInfos = mongoose.model('userInfos', userSchema);
//======================================================
const videoSchema = new mongoose.Schema({
owner:String,
videoID:String,
totalTime: Number,
totalView:Number,
finish: Number,
didFinish:Boolean,
});
const videoInfos = mongoose.model('videoInfos', videoSchema);
由於您使用的是貓鼬,因此您可以像這樣實現它。
像這樣更改架構:
const userSchema = new mongoose.Schema({
username:String,
password:String,
point: Number,
watched: [{ type: Schema.Types.ObjectId, ref: 'videoInfos' }],
running: Array,
});
並像這樣查詢:
userInfos.find({}).populate('watched');
watched
數組應填充videoInfo
數據。
有關更多信息,請查看mongoose populate 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.