Working with MongoDB and Mongoose, let's say ,for example, that I have this schema:
var componentSchema = mongoose.Schema({
name : String,
price : Number
});
var partSchema = mongoose.Schema({
componentId : { type: Schema.Types.ObjectId, ref: 'Component' },
quantity : Number
});
var robotSchema = mongoose.Schema({
name : String,
type : Number,
parts : [partSchema]
});
Every robot needs a set of components to be build.
Since a robot may need more than a single copy of a component (e.g. 10 bolts, 5 screws, 1 transistor...), we store inside the robot model an array of parts, where each part contain the reference to a component plus an additional field, quantity.
Now I'm interested in finding, given an array of component's names (or, eventually, given an array of componentIds) all the robots that I can build with those types of components (notice that a component doesn't include quantity, I just assume that I have an infinite amount of those component), ordered by the one that uses most components of the given array.
Robot A: 2 bolts, 2 transistors
Robot B: 10 bolts, 2 capacitors, 3 bars of plutonium
Robot C: 5 bolts, 1 capacitor, 5 transistors
I have [bolts, capacitors, transistors].
Query results:
Robot C
Robot A
(In this order!)
Is it even possibile with a complex MongoDB query?
Aucun commentaire:
Enregistrer un commentaire