npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@jerrymannel/mongo-express-middleware

v1.1.0

Published

Express middleware that will give you readymade CRUD APIs on MongoDB

Readme

mongo-express-middleware

Express middleware that will give you CRUD APIs on top of any MongoDB collection

Usage

const { MongoClient } = require("mongodb");
const express = require('express');
const app = express();

const MongoExpressMiddleware = require('@jerrymannel/mongo-express-middleware');

const uri = "mongodb://localhost:27017";

const client = new MongoClient(uri);
const database = client.db('test');
const testCollection = database.collection('users');

const crud = new MongoExpressMiddleware(testCollection, null);

console.log(new Date());

app.use(express.json());

app.get('/', crud.find);
app.get('/:id', crud.findById);
app.get('/utils/count', crud.count);
app.post('/', crud.create);
app.put('/:id', crud.update);
app.delete('/', crud.deleteMany);
app.delete('/:id', crud.deleteById);
app.post('/utils/aggregate', crud.aggregate);


app.listen(3000, () => {
	console.log('Example app listening on port 3000!');
});

Install

npm i @jerrymannel/mongo-express-middleware

Documentation

find(req, res)

Query a collection with a given filter, which is send as URL params. This simulates the Mongodb find operation.

| Query | Type | Description | e.g. | |--|--|--|--| | filter | Object | Apply a filter for retrieving data. Accepts any valid MongoDB filter expression. | {"buying_price": {"$ne": 100}} | | select | Object | Select the attributes you want to retrieve. If select is not set, then the whole document is retrieved. Accepts any valid MOngoDB projection expression | {"amount":1, "units":1, "name":1} | | sort | Object | Sorts the data. Accepts MongoDB sort syntax | {"name":-1} | | limit | Number | The maximum number of documents to return. If unspecified, then defaults to 10 | | page | Number | Page based on the limit specified. |

Response

Returns an array of JSONs or an empty array

Filter and select expressions are similar to MongoDB Query and Projection Operators .

How page param works

Page param for find(req, res) works together with filter, limit, and total document count.

Let's assume that the total number of documents in the collection is 10 and your filter condition doesn't change between API calls, then

  • Setting limit=5 and page=1 fetched the first 5 documents.
  • Setting limit=5 and page=2 fetched the next 5 documents. etc

findById(req, res)

Query the collection and find the document with the _id that matches req.params.id

| Query | Type | Default | Description | |--|--|--|--| | isObjectId | Boolean | false | If isObjectId is set then we find the document where _id is of type ObjectId |

Response

Returns a single JSON.

Why isObjectId ?

You can create two documents in MongoDB with the same _id where one is of type string and the other of type ObjectId.

When you create a document using the create(req, res), you can set the _id of the document. If the isObjectId param is not set for create then the user-specified _id is set as type string.

Hence, I have made provision under findById(req, res) to query documents where _id can be of either type.

count(req, res)

Count the number of documents under the collection that matches the specified filter param. If filter is not set then this returns the total number of documents.

| Query | Type | Description | e.g. | |--|--|--|--| | filter | Object | Apply a filter for retrieving data. Accepts any valid MongoDB filter expression. | {"buying_price": {"$ne": 100}} |

Response

Returns a non negative interger value.

create(req, res)

Input - Single JSON or an Array of JSONs

Creates a single or multiple documents when an array of JSONs are passed.

| Query | Type | Default | Description | |--|--|--|--| | isObjectId | Boolean | false | If isObjectId is set then we parse the _id of the document, if present, as type ObjectId |

Response

If a single JSON is given as input then, the response is a JSON document with only the _id of the document that was created.

If an array of JSONs are given as input then,

  • HTTP 200 OK if all the documents where inserted successfully.
  • HTTP 207 if someof the documents got inserted.

Response will be a JSON that gives you the insertedCount and the insertedIds. The documents in the insertedIds will either have the _id or the error at the same index as the input array. e.g.

{
  "insertedCount": 3,
  "insertedIds": [
    {
      "_id": "63deca5b04463760d5bb7bed"
    },
    {
      "_id": "63deca5b04463760d5bb7bee"
    },
    {
      "_id": "63deca5b04463760d5bb7bef"
    }
  ]
}

update(req, res)

Update/replace/upserts a document with the _id that matches req.params.id

| Query | Type | Default | Description | |--|--|--|--| | isObjectId | Boolean | false | If isObjectId is set then we find the document where _id is of type ObjectId | | isReplace | Boolean | false | Replace the existing document in the collection with the document in the payload | | upsert | Boolean | false | Update the document is present, else create a new document. Read more about upsert here. |

Response

JSON that has the _id of the document that was updated/replaced/upserted.

deleteMany(req, res)

Deletes multiple documents that matches the filter query.

Accepts filter as a query param. Refer filter documentation under find

deleteById(req, res)

Deletes a single document with the _id that matches req.params.id.

Accepts isObjectId as a query param. Refer isObjectId documentation under findById

aggregate(req, res)

Input - MongoDB aggregation pipeline

Returns the result of running the MongoDB aggregation pipeline.