MongoDB atlas data API SDK for Node.js
npm install mongodb-data-api





MongoDB Atlas Data API SDK for Node.js.
---
``bash`
npm install mongodb-data-api --save
or
`bash`
yarn add mongodb-data-api
#### Init
`ts
import { createMongoDBDataAPI } from 'mongodb-data-api'
// init by URL Endpoint
const api = createMongoDBDataAPI({
apiKey: '
urlEndpoint: 'https://data.mongodb-api.com/app/
})
// or init by app ID
const api = createMongoDBDataAPI({
apiKey: '
appId: '
})
// specific region and cloud of app
const api = createMongoDBDataAPI({
apiKey: '
appId: '
region: '
cloud: '
})
`
#### Actions
See MongoDB Data API Resources.
- API.findOne
- API.find
- API.insertOne
- API.insertMany
- API.updateOne
- API.updateMany
- API.replaceOne
- API.deleteOne
- API.deleteMany
- API.aggregate
#### Action examples
1. find a single document
`ts`
api
.findOne({
dataSource: '
database: '
collection: '
filter: { name: 'Surmon' }
})
.then((result) => {
console.log(result.document)
})
2. insert a single document
`ts`
api
.insertOne({
dataSource: '
database: '
collection: '
document: {
name: 'Surmon',
age: 19
}
})
.then((result) => {
console.log(result.insertedId)
})
3. run an aggregation pipeline
`ts`
api
.aggregate({
dataSource: '
database: '
collection: '
pipeline: [
{ $match: { status: 'urgent' } },
{ $group: { _id: '$productName', sumQuantity: { $sum: '$quantity' } } }
]
})
.then((result) => {
console.log(result.documents)
})
#### Method chaining
`ts
// select cluster
const clusterA = api.$cluster('a')
// select database
const databaseB = clusterA.$database('b')
// select collection
const collectionC = databaseB.$collection
// data actions
const data = await collectionC.findOne({
filter: {
/.../
}
})
const result = await collectionC.insertOne({
document: {
/.../
}
})
// -------------
// chaining is equivalent to the api call
api.$cluster('a').$database('b').$collection
// the same as
api.findOne
dataSource: 'a',
database: 'b',
collection: 'c',
filter: {}
})
`
#### Specific Action
You can specify the action request to prevent this package from lagging relative to the official one.
`ts`
api.$$action('findOne', {
dataSource: '...',
database: '...',
collection: '...',
filter: {}
})
#### Original Class
You can use the original Class to implement some special requirements.
`ts
import { MongoDBDataAPI } from 'mongodb-data-api'
const customerCollection = new MongoDBDataAPI
{
apiKey: '
appId: '
},
{
dataSource: '
database: '
collection: '
}
)
const customer = await customerCollection.findOne({ ... })
`
`bashinstall dependencies
yarn
Detailed changes for each release are documented in the release notes.