GithubHelp home page GithubHelp logo

mongoose-lookuper's Introduction

Mongoose lookuper

Mongoose plugin that generates an aggregate pipeline for deep search on specified fields

Install

  npm install --save mongoose-lookuper

Usage

  const mongoose = require('mongoose');
  const lookuper = require('mongoose-lookuper');

  const {ObjectId} = mongoose;

  // create models
  mongoose.model('LevelThree', {
    someField: String
  });

  mongoose.model('LevelTwo', {
    fieldInLevelTwo: Number,
    levelThree: {
      ref: 'LevelThree',
      type: ObjectId
    }
  });

  const LevelOneModel = mongoose.model('LevelOne', {
    title: String,
    embeddedObj: {
      embeddedTitle: String,
      embeddedField: {
        levelTwos: [{
          ref: 'LevelTwo',
          type: ObjectId
        }]
      }
    }
  });

  const pipeline = lookuper.getPipeline(LevelOneModel, 'embeddedObj.embeddedField.levelTwos.levelThree');
  const docs = await LevelOneModel.aggregate(pipeline); // docs with lookuped fields

The variable 'docs' contains documents with lookuped fields:

[
  {
    "_id": "5e04a8d9d9bae70a75fb5377",
    "embeddedObj": {
      "embeddedField": {
        "levelTwos": [
          {
            "_id": "5e04a8d9d9bae70a75fb5379",
            "levelTwo": {
              "_id": "5e04a8d9d9bae70a75fb5373",
              "levelThree": {
                "_id": "5e04a8d9d9bae70a75fb5372",
                "levelFour": {
                  "_id": "5e04a8d9d9bae70a75fb5371",
                  "someField": "aspernatur et fugiat",
                  "__v": 0
                },
                "someField": "sit ad dolorem",
                "__v": 0
              },
              "fieldInLevelTwo": "ipsam eum reiciendis",
              "__v": 0
            },
            "lostField": "soluta dicta deserunt"
          },
          {
            "_id": "5e04a8d9d9bae70a75fb5378",
            "levelTwo": {
              "_id": "5e04a8d9d9bae70a75fb5376",
              "levelThree": {
                "_id": "5e04a8d9d9bae70a75fb5375",
                "levelFour": {
                  "_id": "5e04a8d9d9bae70a75fb5374",
                  "someField": "expedita ex aperiam",
                  "__v": 0
                },
                "someField": "placeat ratione facilis",
                "__v": 0
              },
              "fieldInLevelTwo": "beatae iste aspernatur",
              "__v": 0
            },
            "lostField": "et voluptas ipsa"
          }
        ]
      },
      "embeddedTitle": "quam doloribus corporis"
    },
    "title": "minus et amet",
    "__v": 0
  }
]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.