@tomsd/mongodbclient
v4.0.2
Published
It's a handy mongodb client for easy-use.
Downloads
192
Readme
@tomsd/mongodbclient
It's a handy mongodb client for easy-use.
See mongodbclient.netlify.app for details.
Installation
npm install @tomsd/mongodbclient
Usage
import { MClient } from "@tomsd/mongodbclient";
const uri = "mongodb+srv://...";
const dbName = "mydb";
const collectionName = "mycollection";
const mdbc = new MClient(uri, dbName, collectionName);
const items = [
{ name: "alice" },
{ name: "bob" },
{ name: "charlie" },
{ name: "alice" }
];
(async () => {
const { insertedCount } = await mdbc.insertMany(items);
console.log(insertedCount); // 4
const docs = await mdbc.read();
console.log(docs);
const { upsertedCount, modifiedCount } = await mdbc.upsert({
...docs[0],
name: "david"
});
console.log(`upsertedCount: ${upsertedCount}, modifiedCount: ${modifiedCount}`);
const names = await mdbc.distinct("name");
console.log(`distinct names: ${names.length}`); // 4
const itemLength = await mdbc.count();
console.log(`count: ${itemLength}`); // 4
const { deletedCount } = await mdbc.remove({});
console.log(`deletedCount: ${deletedCount}`); // 4
})();