如何查询具有多个模式或模型的集合?

how to query collection which has multiple schemas or models?

我在一个集合中插入了几个使用不同模式(或模型)的文档,我需要通过所有文档查询该集合。 查询是 {reason: {$or: [1, 2]}} 和 {_id: objectid('xxx')}。 我该怎么办?

var eqAddPendingSchema = new Schema({
    idString: String,
    name: String,
    category: String,
    life_cycle: Number,
    location: String,
    purchase_date: String,
    description: String,
    image: String,
    video: String,
    reason: Number,
    comment: String,
    created_at: Number,
    updated_at: Number
}, {collection: 'equipments_pending'});
var eqBookPendingSchema = new Schema({
    eq_dbId: String,
    user_dbId: String,
    from: Number,
    to: Number,
    reason: Number,
    created_at: Number,
    updated_at: Number
}, {collection: 'equipments_pending'});
inspectionPendingSchema = new Schema({
    user_dbId: String,
    eq_dbId: String,
    status: Number,
    comment: String,
    reason: Number,
    timestamp: Number,
    created_at: Number,
    updated_at: Number
}, {collection: 'equipment_pending'});
maintainPendingSchema = new Schema({
    user_dbId: String,
    eq_dbId: String,
    status: Number,
    comment: String,
    reason: Number,
    timestamp: Number,
    created_at: Number,
    updated_at: Number
}, {collection: 'equipment_pending'});

我想知道你为什么要专门为 inspectionPendingSchema 和 maintain booth 相同,请寻找更好的设计。

我不知道这是不是打字错误,您定义了两个与 equipments_pending 链接的模式和另外两个与 equipment_pending[= 链接的模式12=]

定义所有模式后,您必须定义模块

var aModule = mongoose.model('eqAddPendingSchema', eqAddPendingSchema);
var bModule = mongoose.model('eqBookPendingSchema', eqBookPendingSchema);


var cModule = mongoose.model('inspectionPendingSchema', inspectionPendingSchema);
var dModule = mongoose.model('maintainPendingSchema', maintainPendingSchema);

查找 reason 是否等于 1 或 2 且 id 是否等于 x

aModule.find({ reason: { $in: [1, 2] } , _id: "5d641cd4d65a0b18b22ddd86" } ,(e,d) =>{
        console.log(d);
       ////// change code
        var newd = d;
        newd.newKey = 'newVariable';
        res.send(newd);
      ////////////change end
    });