简体   繁体   中英

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). Please use syntax like videoInfos.find().exce if you can.

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.

For more, take a look at mongoose populate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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