如何查询具有多个模式或模型的集合? [英] how to query collection which has multiple schemas or models?
问题描述
我在一个集合中插入了几个使用不同架构(或模型)的文档,我需要在所有文档中查询该集合. 查询是{原因:{$ or:[1,2]}}和{_id:objectid('xxx')}. 我该怎么办?
I inserted several documents that use different schemas(or models) in one collection and I need to query the collection through all documents. query is {reason: {$or: [1, 2]}} and {_id: objectid('xxx')}. how can i do?
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和维护摊位相同,请寻找更好的设计.
I'm wondering why you are making many schema's specially for inspectionPendingSchema and maintain booth are the same, please find better design.
我不知道这是否是拼写错误,您是否已定义了两个与equipments_pending关联的架构和另外两个与equipment_pending关联的架构
定义所有模式后,您必须定义模块
After defining all schema's you have to define modules
var aModule = mongoose.model('eqAddPendingSchema', eqAddPendingSchema);
var bModule = mongoose.model('eqBookPendingSchema', eqBookPendingSchema);
var cModule = mongoose.model('inspectionPendingSchema', inspectionPendingSchema);
var dModule = mongoose.model('maintainPendingSchema', maintainPendingSchema);
查找原因是否等于1或2且id等于x
to find if reason equal 1 or 2 and id equal 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
});
这篇关于如何查询具有多个模式或模型的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!