
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
mongodb-utils
Advanced tools
MongoDB utils library for node
npm install --save mongodb-utils
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' })
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(doc): 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(query = { key: value } || string || ObjectID, doc): 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(query = { key: value } || string || ObjectID, doc): 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.update('59c0de2dfe8fa448605b1d89', { username: 'terrajs2' })
// Update the document that match the query { username: 'terrajs2' } and update its username or create it if not exist
await collection.utils.update({ username: 'terrajs2' }, { username: 'terrajs' })
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(query = { key: value } || string || ObjectID, [options = { fields: ..., limit: ..., offset: ..., sort: ... }]): Promise<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 defaultoffset
: Nb of docs to skpi, default: 0
sort
: Sort criteria (same as sort
method from mongo cursor), default: {}
// We find document that match the query { username: new RegExp(/^test/g) }, options with { username: 1, createdAt: 1 } projection and limit at 1 element
const request = await userCollection.mono.find({
username: new RegExp(/^test/g)
}, {
fields: ['username', 'createdAt'],
limit: 1
})
FAQs
Utils for mongodb for nodejs
The npm package mongodb-utils receives a total of 7 weekly downloads. As such, mongodb-utils popularity was classified as not popular.
We found that mongodb-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
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.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.