admin管理员组文章数量:1352869
I am a new to nedb. Its a kinda what sqlite is for sql munity but for the node.js munity. []
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...
I am a new to nedb. Its a kinda what sqlite is for sql munity but for the node.js munity. [https://github./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...
Share Improve this question edited May 31, 2014 at 4:55 Neil Lunn 151k36 gold badges355 silver badges325 bronze badges asked May 31, 2014 at 4:51 AdwinAdwin 1952 gold badges6 silver badges21 bronze badges2 Answers
Reset to default 10This 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.
//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); });
本文标签: javascriptNedb Multiple Collection Single DatastoreStack Overflow
版权声明:本文标题:javascript - Nedb Multiple Collection Single Datastore - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743910605a2560328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论