问题
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'});
回答1:
I'm wondering why you are making many schema's specially for inspectionPendingSchema and maintain booth are the same, please find better design.
I don't know if it's typo error or not you have defined two schema's linked with equipments_pending and other two linked with 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);
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
});
来源:https://stackoverflow.com/questions/57661644/how-to-query-collection-which-has-multiple-schemas-or-models