Arrow keys: ← previous · → next 18 of 35
@nodedk/mongodb
0.1.2 stable

mongodb#

Create MongoDB driver clients and object IDs.

Requirements#

A MongoDB server for database operations.

Install#

npm i @nodedk/mongodb

Usage#

var { client, ObjectId } = require('@nodedk/mongodb')

async function main() {
  var connection = client('mongodb://127.0.0.1:27017')
  await connection.connect()

  try {
    var db = connection.db('example')
    var id = new ObjectId('507f1f77bcf86cd799439011')
    var user = await db.collection('users').findOne({ _id: id })
    console.log(user)
  } finally {
    await connection.close()
  }
}

main().catch(console.error)

API#

client(url, options)#

Returns a native MongoClient synchronously. url is the connection string; optional options are passed unchanged to the driver constructor, including authentication, TLS, pool size, and timeout settings. Creating the client does not connect it.

Use await connection.connect(), connection.db(name, options), and await connection.close(force) to manage its lifetime. Database, collection, cursor, and session APIs are the native MongoDB driver APIs.

ObjectId#

Re-exports the driver's ObjectId class. new ObjectId() creates an ID; pass a 24-character hexadecimal string, 12-byte byte array, or existing ObjectId to construct one from a value. ObjectId.isValid(value) checks accepted input. Instances provide toHexString(), toString(), equals(otherId), and getTimestamp().

Created by Vidar Eldøy