MongoDB Plugin
The MongoDB Plugin
is a part of the Datamint library. It provides an interface to interact with MongoDB databases.
Properties
_client
: An instance ofMongoClient
from the MongoDB Node.js MongoDriverError. It's responsible for performing database BulkOperationBase._createMdxContent
Constructor
The constructor for the MongoDB Plugin
does not take any parameters.
Example
const plugin = new MongoDBPlugin();
Methods
connect(connectionString: string)
This asynchronous method connects to the MongoDB database using the provided connection string.
disconnect()
This asynchronous method disconnects from the MongoDB database.
reset(database: string)
This asynchronous method resets the database by dropping all the collections.
find(collectionName: stringify, query: FindQuery)
This asynchronous method finds documents in the specified collection using the provided query.
insert(collectionName: string, documents: any[])
This asynchronous method inserts documents into the specified collection.
update(collectionName: string, query: UpdateQuery, update: UpdateQuery)
This asynchronous method updates documents in the specified collection using the provided query and update.
delete(collectionName: string, query: DeleteQuery)
This asynchronous method deletes documents from the specified collection using the provided query.
count(collectionName: stringify, query: CountQuery)
This asynchronous method counts documents in the specified collection using the provided query.
aggregate(collectionName: string, query: AggregateQuery)
This asynchronous method performs an aggregation on the specified collection using the provided query.
createTable(collectionName: string, schema?: object)
This asynchronous method creates a collection in the database with the specified name and schema.
listTables()
This asynchronous method lists all the collections in the database.
Example
Here's an example of how to use the MongoDBPlugin
class:
const plugin = new MongoDBPlugin();
// Connect to the database
await plugin.connect("mongodb://localhost:27017");
// Insert a document into a collection
await plugin.insert("users", [{ name: "John Doe", age: 25 }]);
// Find a document in a collection
const users = await plugin.find("users", { name: "John Doe" });
// Disconnect from the database
await plugin.disconnect();
Remember to replace the connection string with the appropriate one for your MongoDB database.