Multiple collection single datastore

Hey,
I am a new to 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 did 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, could you please show me how to achieve this?
Thank you…

1 possible answer(s) on “Multiple collection single datastore

  1. You can’t do it this way. As docs tells us there is one DB instance (and therfore file) per one document collection.

    // Of course you can create multiple datastores if you need several
    // collections. In this case it's usually a good idea to use autoload for all collections.
    db = {};
    db.users = new Datastore('path/to/users.db');
    db.robots = new Datastore('path/to/robots.db');
    
    // You need to load each database (here we do it asynchronously)
    db.users.loadDatabase();
    db.robots.loadDatabase();

    Workaround would be to mix different document types together in one collection and then filter only one type, however this is a little troublesome.

    db.insert([{ type: "foo" }, { type: "foo" }], function (err, newDocs) {
    });
    db.insert([{ type: "bar" }, { type: "bar" }], function (err, newDocs) {
    });
    db.find({ type: "foo" }, function (err, docs) {
      // will return all objects of type foo from db (so like having collection in a collection)
    });