【问题标题】:How to filter a schema based on other schema如何根据其他模式过滤模式
【发布时间】:2021-09-04 05:38:54
【问题描述】:

我有 2 个架构

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const {ObjectId} = mongoose.Schema;

const matchList = new Schema({
    user:[{
        type: ObjectId,
        ref:'User'
    }]
});


const user = new Schema({
    name:{
        type:String
    }
});

ma​​tchlist 包含一个用户字段,它是来自用户模式的用户 ID 数组。 如何从用户架构中获取 id 不在匹配列表集合中的所有用户?

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    首先你从 matchList 集合中获取一个文档。

    const _matchList = matchList.findOne();
    

    接下来,您使用 $nin 运算符过滤用户集合以按数组过滤,在本例中为 ObjectId 数组。

    const _usersNotMatch = user.find({ _id: { $nin: _matchList.user }}).
    

    那么你有 id 不在匹配列表集合中的用户

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      相关资源
      最近更新 更多