Reputation: 195
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...
Upvotes: 4
Views: 4743
Reputation: 2418
//create multiple collections
var Datastore = require('nedb');
var db = {};
db.users = new Datastore('path/to/users.db');
db.robots = new Datastore('path/to/robots.db');
//access it
db.users.loadDatabase();
db.robots.loadDatabase();
//find some documents
db.users.find({name: "john"}, function (err,docs){ console.log(docs); });
Upvotes: 0
Reputation: 151122
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.
Upvotes: 10