find-by-reference-in-mongoose
v1.0.0
Published
Add Mongoose support for finding in reference
Downloads
114
Maintainers
Readme
find-by-reference-in-mongoose
This is a Mongoose plugin that allows your Mongoose to support lookup on reference fields.
Reference field is like this:
{
type: MongooseSchema.Types.ObjectId,
ref: 'XXX',
}
Its principle is to parse your find request. When it finds that you want to filter the value of the reference field, it will read the model corresponding to the reference field and perform the search, obtain the id array that matches the filter, and then put the reference field The filter of the value is replaced by the judgment of whether the value of the reference field is in the id array.
install
npm i -S find-by-reference-in-mongoose
usage
The find-by-reference-in-mongoose
module exposes a single function that you can
pass to Mongoose schema's plugin()
function.
const { FindByReferenceInMongoose } = require("find-by-reference-in-mongoose");
const schema = new mongoose.Schema({
/* ... */
});
schema.plugin(FindByReferenceInMongoose);
Then, you can use it like this.
const result = await catModel
.find({
$and: {
parents: {
"owner.name": "Dean",
},
sex: 0,
},
})
.exec();
Its conditions will be automatically replaced with:
const newConditions = {
$and: {
parents: {
$in: [
/* ObjectIDs for Eligible Cats */
],
},
sex: 0,
},
};