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 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongodb-utils

v1.1.6

Published

Utils for mongodb for nodejs

Downloads

108

Readme

MongoDB utils for Node.js

npm version Travis Coverage license

Installation

npm install --save mongodb-utils

Usage

MongoDB utils overide the collection class by adding an utils object that will expose all the MongoDB utils methods:

const mongoUtils = require('mongodb-utils')

const collection = mongoUtils(db.collection('users'))

// We can now access to mongodb-utils method from .utils
const user = await collection.utils.get({ usermane: 'terrajs' })

Methods

get

get(query = { key: value } || string || ObjectID, [fields]): Promise<doc>

Return a document that match the specific identifier (_id by default) or the query:

// Get the document that match the query { _id: ObjectID('59c0de2dfe8fa448605b1d89') }
collection.utils.get('59c0de2dfe8fa448605b1d89')

// Get the document that match the query { username: 'terrajs' }
collection.utils.get({ username: 'terrajs' })

// Get the document that match the query & return only its _id
collection.utils.get({ username: 'terrajs' }, { _id: 1 })
// Get the document that match the query & return only its _id (works with array too)
collection.utils.get({ username: 'terrajs' }, ['_id'])

create

create(doc: object): Promise<doc>

Insert a document into the collection and add createdAt and updatedAt properties:

// Add a document into the collection and return the created document
const user = await collection.utils.create({ username: 'terrajs' })

update

update(query = { key: value } || string || ObjectID, doc: object): Promise<doc>

Update a specific document and update the updatedAt value

// Update the document that match the query { _id: ObjectID('59c0de2dfe8fa448605b1d89') } and update its username
await collection.utils.update('59c0de2dfe8fa448605b1d89', { username: 'terrajs2' })

// Update the document that match the query { username: 'terrajs2' } and update its username
await collection.utils.update({ username: 'terrajs2' }, { username: 'terrajs' })

upsert

upsert(query = { key: value } || string || ObjectID, doc: object): Promise<doc>

Update or create a document if not exist Add the createdAt if document not exist

// Update the document that match the query { _id: ObjectID('59c0de2dfe8fa448605b1d89') } and update its username or create it if not exist
await collection.utils.upsert('59c0de2dfe8fa448605b1d89', { username: 'terrajs2' })

// Update the document that match the query { username: 'terrajs2' } and update its username OR create it if not found
await collection.utils.upsert({ username: 'terrajs2' }, { username: 'terrajs' })

remove

remove(query = { key: value } || string || ObjectID): Promise<boolean>

Remove a document that match the specific identifier (_id by default) or the query:

// Remove the document that match the query { _id: ObjectID('59c0de2dfe8fa448605b1d89') }
const result = collection.utils.remove('59c0de2dfe8fa448605b1d89')

// Remove the document that match the query { username: 'test' }
collection.utils.remove({ username: 'test' })

find

find(query = { key: value } || string || ObjectID, [options = { fields: ..., limit: ..., offset: ..., sort: ... }]): cursor

The find method return a mongo cursor from a specific query and options.

Options:

  • fields: Array of keys (['username', ...]) to return OR a MongoDB projection ({ field1: 1, ... }), default: {}
  • limit: Nb of docs to return, no limit by default
  • offset: Nb of docs to skpi, default: 0
  • sort: Sort criteria (same as sort method from mongo cursor), default: {}
// Find documents that matches the query { username: new RegExp(/^test/g) }, options with { username: 1, createdAt: 1 } projection and limit at 10 elements
const users = await userCollection.mono.find({
  username: new RegExp(/^test/g)
}, {
  fields: ['username', 'createdAt'],
  limit: 10
}).toArray()