简体   繁体   English

如何使用排除搜索猫鼬

[英]How to search on mongoose with exclude

i have 2 Schema like bellow, i need to get 50 item from videoInfos which not have in userInfos.watched (this array content _id of videoInfos).我有 2 个架构,如下所示,我需要从 videoInfos 中获取 50 个项目,而 userInfos.watched 中没有这个项目(这个数组内容是 videoInfos 的内容 _id)。 Please use syntax like videoInfos.find().exce if you can.如果可以,请使用像 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);

Since you are using mongoose you can achieve it like this.由于您使用的是猫鼬,因此您可以像这样实现它。

Change the schema like this:像这样更改架构:

const userSchema = new mongoose.Schema({
    username:String,
    password:String,
    point: Number,
    watched: [{ type: Schema.Types.ObjectId, ref: 'videoInfos' }],
    running: Array,
});

And query like this:并像这样查询:

userInfos.find({}).populate('watched');

The watched array should be populated with videoInfo data. watched数组应填充videoInfo数据。

For more, take a look at mongoose populate .有关更多信息,请查看mongoose populate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM