1.0.6 • Published 5 years ago

mongodb-keyv v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Simple Key-Value Storage For MongoDB.

Steps:-

1> Install modules

  • MongoDB Driver
npm install mongodb
  • MongoDB Key-Value Storage
npm install mongodb-keyv

2> Connect to MongoDB And Setup Keyv

const { Client: Keyv } = require('mongodb-keyv');
const { MongoClient } = require('mongodb');

const uri = ""; //MongoDB URI here.

const client = new MongoClient(uri, { useUnifiedTopology: true });

client.connect(err => {
    if (err) return console.log(err);
    const settings = new Keyv(client.db().collection("settings"));
    client.close();
});

Methods

MethodsArgumentsDescription
init()Loads the data in cache value
set()id, key, valueSets the data
get()id, key, defaultValueGets the data (defaultValue is optional)
delete()id, keyDeletes the data
clear()idClears the entire data

Examples:-

init()

const settings = new Keyv(client.db().collection("settings"));
settings.init();

set()

const settings = new Keyv(client.db().collection("settings"));

const marks = {
	english: 88,
	physics: 90
	grade: 'A+'
}

settings.set('Students', 'Name', 'Ayush Chowdhury');
settings.set('Students', 'Class', 9);
settings.set('Students', 'Marks', marks);
settings.set('Students', 'Attendence', ['Present', 'Absent', 'Absent']);

get()

settings.get('Students', 'Name');
settings.get('Students', 'Marks', 0); // If there is no key named 'Marks', it will return the default value. In this case the deafult value is 0. 

delete()

settings.delete('Students', 'Attendence');

clear()

settings.clear('Students');
1.0.6

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago