Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nedb Multiple Collection Single Datastore

I am a new to nedb. Its a kinda what sqlite is for sql community but for the node.js community. [https://github.com/louischatriot/nedb]

I wanted to ask is possible to have multiple collections in a single database file (datastore). If there is, could please show me some code sample on how to go about it? I have tried this:

var Datastore = require('nedb'),
    databaseURL="tudls.db",
    db = new Datastore({filename: databaseURL, autoload: true});

This creates a single datastore called db. From the documentation, I saw that nedb is mongo-like. So to insert a record I tried this:

 app.post('/todos', function(req, res){
        var task = req.body.text;
        db.todols.insert({text: task, done: false}, function(err, saved){
            if(err||!saved){
                res.send("Task not saved...");
            }
            res.send("Task saved...");});
    });

However, I get a 'cannot call method insert of undefined.' I thought that if I call the collection name (todols) when inserting a record it would work so that I can proceed to add another collection to the datastore (db.user) but I was mistaken.

Hence, is it possible to have multiple collections in a single datastore or am I to have a datastore for each collection? If it is possible, does anyone know how to achieve this? Thank you...

like image 953
Adwin Avatar asked Dec 26 '22 08:12

Adwin


1 Answers

This really is a "lite" interpretation of MongoDB and as such there really isn't the same concept of "databases" and "collections" that exists in the full featured product, as well as omitting a lots of other features.

If you want things to appear as if you do have various "collections", then as suggested in the manual page you define various DataStore objects within a structure to make things look that way:

var db = {};
db.todols = new DataStore('/path/to/todols.db');
db.other = new DataStore('/path/to/other.db');

That makes it appear that you have "collections" which are in fact to "neDB" actually just DataStore objects.

like image 134
Neil Lunn Avatar answered Dec 28 '22 05:12

Neil Lunn