1.0.8 • Published 4 months ago

mongoose-find-by-reference v1.0.8

Weekly downloads
-
License
Apache 2.0
Repository
github
Last release
4 months ago

mongoose-find-by-reference

npm version contributions welcome Downloads

English | 简体中文

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 mongoose-find-by-reference

usage

The mongoose-find-by-reference module exposes a single function that you can pass to Mongoose schema's plugin() function.

const { MongooseFindByReference } = require("mongoose-find-by-reference");
const schema = new mongoose.Schema({
  /* ... */
});
schema.plugin(MongooseFindByReference);

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,
  },
};

need help

Now this project needs everyone's help, and there is currently a lack of testing.

1.0.8

4 months ago

1.0.7

4 months ago

1.0.6

9 months ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago