1.0.1 • Published 2 years ago

mongo-data-api v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

MongoDB data API

Some preconfigured utility functions to easily request the MongoDB Data API.

Setup

First add the following variables and their values to your .env file

MONGO_APP_ID=
MONGO_API_KEY=
MONGO_CLUSTER_NAME=
MONGO_DATABASE=

How to use

// import the functions you need
import {
  findMany,
  findOne,
  updateMany,
  updateOne,
  replaceOne,
  deleteMany,
  deleteOne,
  aggregate,
} from "mongo-data-api";

// all functions take 2 arguments ("collectionName", {options object}) and return a promise
// Only the data of the response is returned, not the whole response
(async () => {
  const data = await findMany("collection", { limit: 3 });
  console.log(data); // { documents: [...] }
})();

Details

Every function takes 2 arguments: the collection that is requested (mandatory), and an object with various options (optional, depending on the type of request). Check also MongoDB documentation for more details.

Examples:

await findOne("tasks", { filter: { text: "Do the dishes" } });
await findMany("tasks"); // returns all documents in "tasks" collection
await insertOne("tasks", {
  document: { completed: false, text: "Go shopping" },
});
FunctionOptionsTypeNecessityResponse Data
findManyfilterobjectOptional{ documents: { ... }, ... }
projectionobjectOptional
sortobjectOptional
limitnumberOptional
skipnumberOptional
findOnefilterobjectOptional{ document: {...} }
projectionobjectOptional
insertOnedocumentobjectRequired{ insertedId: "..." }
insertManydocumentsarray of objectsRequired{ insertedIds: "...", "...", ... }
updateOnefilterobjectRequired{ matchedCount: 0, modifiedCount: 0, upsertedId: "..." }
updateobjectRequired
upsertbooleanOptional
updateManyfilterobjectRequired{ matchedCount: 0, modifiedCount: 0, upsertedId: "..." }
updateobjectRequired
upsertbooleanOptional
replaceOnefilterobjectRequired{ matchedCount: 0, modifiedCount: 0, upsertedId: "..." }
replacementobjectRequired
upsertbooleanOptional
deleteOnefilterobjectRequired{ deletedCount: 1 }
deleteManyfilterobjectRequired{ deletedCount: 26 }
aggregatepipelinearray of objectsRequired{ documents: { ... }, ... }