Simple. Easy. Light

MongoDB Model.

Beta
Simplicity is power!

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!

Install MongoDB Model with yarn
copy
yarn add @mongodb-model/model
or with npm
copy
npm install @mongodb-model/model
Start using it

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!

copy
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

    copy
    
    User.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

    copy
    
    User.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

    copy
    
    User.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

    copy
    
    User.find()
        .then(users => console.log('all users found', users))
        .catch(error => console.log('error finding all users', error));
                

    Or use event

    copy
    
    User.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

    copy
    
    User.find({},{},(error, users) => {
        if(error) return console.log('error finding all users', error);
        console.log('all users found', users);
    });
  • UPDATE

    Use promise

    copy
    
    User.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

    copy
    
    User.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

    copy
    
    User.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

    copy
    
    User.deleteById('6476fe3e6e636c2f079ecacc')
        .then(result => console.log('user deleted by id', result))
        .catch(error => console.log('error deleting user by id', error));
                

    Or use event

    copy
    
    User.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

    copy
    
    User.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

  1. Install Mongodb Model
    copy
    yarn add @mongodb-model/model
    or
    copy
    npm install @mongodb-model/model
  2. Type the command 'model' in your terminal
    copy
    model

You should see something like this in your terminal

macbook and a clock
;