You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TCP connections over Mongodb Atlas is not possible (e.g. Serverless runtime like Cloudflare Workers), while still wanting to use similar MongoDB driver syntax.
🚨 Importart 🚨: If you select "Local (single region)" when you enable Data API
Make sure to pass the App Region as well!
And Get the API Key here
2. Installation
npm install mongo-http --save
3. Initialization
Depending on your needs, you can either initialize a client, database, or collection
You can initialize a client for connecting to multiple databases
import{initClient}from'mongo-http';constclient=initClient({appId: process.env.appId,apiKey: process.env.apiKey,// Important! Pass `appRegion` if you deploy Data API as "Local (single region)"// See above "1. Setup MongoDB Atlas to get the App ID and API Key (and App Region)"appRegion: process.env.appRegion,});constdb=client.database({databaseName: process.env.databaseName});constresult=awaitdb.collection('articles').find({filter: {$or: [{categories: {$in: ['javascript','reactjs','nodejs','mongodb']}}],},});
... Or, Initialize a database
import{initDatabase}from'mongo-http';constdb=initDatabase({appId: process.env.appId,apiKey: process.env.apiKey,// Important! Pass `appRegion` if you deploy Data API as "Local (single region)"// See above "1. Setup MongoDB Atlas to get the App ID and API Key (and App Region)"appRegion: process.env.appRegion,databaseName: process.env.databaseName||'',});constresult=awaitdb.collection('articles').find({});
... Or, Initialize a collection
import{initCollection}from'mongo-http';constarticlesCollection=initCollection({appId: process.env.appId,apiKey: process.env.apiKey,// Important! Pass `appRegion` if you deploy Data API as "Local (single region)"// See above "1. Setup MongoDB Atlas to get the App ID and API Key (and App Region)"appRegion: process.env.appRegion,databaseName: process.env.databaseName,collectionName: 'articles',});constresult=awaitarticlesCollection.find({});
API
.findOne({ filter, projection })
Example
const{ isSuccess, document, error }=awaitdb.collection('articles').findOne({filter: {$or: [{creator: 'Patrick Chiu'},{title: 'Migrating a Node.js App to Cloudflare Workers From Heroku'}],},projection: {title: 1,creator: 1,guid: 1,categories: 1},});
Parameter
Parameter
Type
Default Value
Description
filter
object
{}
A MongoDB Query Filter. The findOne action returns the first document in the collection that matches this filter. If you do not specify a filter, the action matches all document in the collection.
projection
object
{}
A MongoDB Query Projection. Depending on the projection, the returned document will either omit specific fields or include only specified fields or values)
Return
Field
Type
Description
isSuccess
boolean
Whether the database operation successful or not
document
object / null
If a document is matched, an object is returned If not matched, a null is returned
A MongoDB Query Filter. The find action returns documents in the collection that match this filter. If you do not specify a filter, the action matches all document in the collection. If the filter matches more documents than the specified limit, the action only returns a subset of them. You can use skip in subsequent queries to return later documents in the result set.
projection
object
{}
A MongoDB Query Projection. Depending on the projection, the returned document will either omit specific fields or include only specified fields or values)
sort
object
{}
A MongoDB Sort Expression. Matched documents are returned in ascending or descending order of the fields specified in the expression.
limit
number
1000
The maximum number of matched documents to include in the returned result set. Each request may return up to 50,000 documents.
skip
number
0
The number of matched documents to skip before adding matched documents to the result set.
Return
Field
Type
Description
isSuccess
boolean
Whether the database operation successful or not
documents
array of object(s) / empty array
If document(s) are matched, an array of object(s) is returned If no matches, an empty array is returned
The upsert flag only applies if no documents match the specified filter. If true, the updateOne action inserts a new document that matches the filter with the specified update applied to it.
Return
Field
Type
Description
isSuccess
boolean
Whether the database operation successful or not
matchedCount
number
The number of documents that the filter matched
modifiedCount
number
The number of matching documents that were updated
The upsert flag only applies if no documents match the specified filter. If true, the updateMany action inserts a new document that matches the filter with the specified update applied to it.
Return
Field
Type
Description
isSuccess
boolean
Whether the database operation successful or not
matchedCount
number
The number of documents that the filter matched
modifiedCount
number
The number of matching documents that were updated