Socket
Book a DemoInstallSign in
Socket

@cme-pro/mongoose-search

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cme-pro/mongoose-search

Mongoose schema plugin to search on multiple fields. Support search on ObjectID, Regex or Mongodb text indexes

1.0.0
latest
Source
npmnpm
Version published
Maintainers
2
Created
Source

npm node tests downloads

Mongoose schema plugin to search on multiple fields. Supports:

  • Search on ObjectID
  • Regex search, or custom search
  • MongoDB text indexes search (MongoDB >= 2.4, mongoose >=3.6).

There are already multiple plugins to manage search, but most of them are handling too much (like filtering or pagination). This plugin only build the query related to a fulltext search, and let other plugins handle their part (pagination, ...).

Installation

$ npm install @cme-pro/mongoose-search --save

or

$ yarn add @cme-pro/mongoose-search

Overview

Adding plugin to the schema

const mongoose = require("mongoose");
const mongooseSearch = require("@cme-pro/mongoose-search");
const { Schema } = mongoose;

const BlogPost = new Schema({
  title: { type: String, searchable: true },
  body: { type: String, searchable: (q: string) => new RegExp(`${q}`) },
  excerpt: { type: String }
});

BlogPost.index({ title: "text", body: "text" });

const BlogPostModel = mongoose.model("Post", BlogPost);

Usage

Simple search:

const query = BlogPostModel.searchQuery("reactjs");

/**
  query = {
      $or: [
        {
          $text: { $search: "reactjs" }
        },
        {
          title: /^reactjs/
        },
        {
          description: /reactjs/
        }
      ]
    }
*/

Search on ObjectID:

const query = BlogPostModel.searchQuery("5cfa2debabe4d93a5b35897c");

/**
  query = { $_id_: "5cfa2debabe4d93a5b35897c" }
*/

Custom search

const query = BlogPostModel.searchQuery("reactjs", { fields: { title: true } });

/**
  query = {
      $or: [
        {
          $text: { $search: "reactjs" }
        },
        {
          title: /^reactjs/
        }
      ]
    }
*/

Keywords

mongoose

FAQs

Package last updated on 08 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.