How it works
Experience the true power of the MongoDB Native Node.js Driver! Say goodbye to the complexity and clumsiness of MongooseJs, as we simplify your development process. Don't just take our word for it—witness the remarkable capabilities firsthand!
yarn add @mongodb-model/model
npm install @mongodb-model/model
Instantiate the Model class by providing an object with your database URL and collection name as keys. This will give you the corresponding collection model!
const Model = require('@mongodb-model/model');
const User = new Model({url: 'mongodb://127.0.0.1:27017/blog', collection: 'users'});
By initializing the Model class, the database connection process is automated, eliminating the need for manual handling. The instantiation of the Model class seamlessly takes care of the database connection. Additionally, using a schema is entirely optional!
That's it! Now use your model and start interacting with your MongoDB Database!
-
CREATE
Use promise
copyUser.create({firstname: 'John', lastname: 'Doe', email: 'john.doe@gmail.com'}) .then(result => console.log('user created', result)) .catch(error => console.log('error creating user', error));
Or use event
copyUser.create({firstname: 'John', lastname: 'Doe', email: 'john.doe@gmail.com'}); User.on('create', result => console.log('user created', result)); User.on('create-error', error => console.log('error creating user', error));
Or use callback: when using callback, all parameters (both optional and non-optional) are passed
copyUser.create({firstname: 'John', lastname: 'Doe', email: 'john.doe@gmail.com'},{},(error, result) => { if(error) return console.log('error creating user', error); console.log('user created', result); });
-
READ
Use promise
copyUser.find() .then(users => console.log('all users found', users)) .catch(error => console.log('error finding all users', error));
Or use event
copyUser.find(); User.on('find', users => console.log('all users found', users)); User.on('find-error', error => console.log('error finding all users', error));
Or use callback: when using callback, all parameters (both optional and non-optional) are passed
copyUser.find({},{},(error, users) => { if(error) return console.log('error finding all users', error); console.log('all users found', users); });
-
UPDATE
Use promise
copyUser.updateById('6476fe3e6e636c2f079ecacc', {firstname: 'John', lastname: 'Doe', email: 'john.doe@gmail.com'}) .then(result => console.log('user updated by id', result)) .catch(error => console.log('error updating user by id', error));
Or use event
copyUser.updateById('6476fe3e6e636c2f079ecacc', {firstname: 'John', lastname: 'Doe', email: 'john.doe@gmail.com'}); User.on('updateById', result => console.log('error updating user by id', result)); User.on('updateById-error', error => console.log('user updated by id', error));
Or use callback: when using callback, all parameters (both optional and non-optional) are passed
copyUser.updateById('6476fe3e6e636c2f079ecacc', {firstname: 'John', lastname: 'Doe', email: 'john.doe@gmail.com'},{},(error, result) => { if(error) return console.log('error updating user by id', error); console.log('user updated by id', result); });
-
DELETE
Use promise
copyUser.deleteById('6476fe3e6e636c2f079ecacc') .then(result => console.log('user deleted by id', result)) .catch(error => console.log('error deleting user by id', error));
Or use event
copyUser.deleteById('6476fe3e6e636c2f079ecacc'); User.on('deleteById', result => console.log('user deleted by id', result)); User.on('deleteById-error', error => console.log('error deleting user by id', error));
Or use callback: when using callback, all parameters (both optional and non-optional) are passed
copyUser.deleteById('6476fe3e6e636c2f079ecacc',{},(error, result) => { if(error) return console.log('error deleting user by id', error); console.log('user deleted by id', result); });
Mongodb-model leverages the power of the MongoDB Native Node.js Driver, simplifying its complexities. It utilizes the same set of methods with identical parameters, seamlessly integrating with the Mongodb Native Node.js Driver. When employing the callback style, an additional callback function is included for flexibility.
CLI in terminal screenshot
- Install Mongodb Model
copyoryarn add @mongodb-model/model
copynpm install @mongodb-model/model
- Type the command 'model' in your terminal
copymodel
You should see something like this in your terminal