framework
framework copied to clipboard
[nosql] does the database.view really work?
I test the code in the document, it doesn't work
the db file users.nosql:
{"name":"Anna", "sex":"female", "age":14}
{"name":"Anna", "sex":"female", "age":14}
{"name":"Anna", "sex":"male", "age":14}
{"name":"Anna", "sex":"male", "age":14}
the function in the controller:
function test() {
var self = this
var nosql = DB('users');
// Creates a view "women"
nosql.view('women').make(function(builder) {
builder.where('sex', 'female');
builder.sort('age');
});
// Creates a view "men"
nosql.view('men').make(function(builder) {
builder.where('sex', 'male');
builder.sort('age');
});
nosql.find('women').search('name', 'Anna').callback(function(err, response) {
console.log('All women with name "Anna"', response);
});
}
and the result:
ShinnodeMacBook-Pro-2:server shinno$ node-dev app.js
All women with name "Anna" []
Hi @yexing
it works as expected but you need to create the view prior to any operations on DB.
So create for example some definition file, dbviews.js with bellow code
// Creates a view "women"
NOSQL('users').view('women').make(function(builder) {
builder.where('sex', 'female');
builder.sort('age');
});
// Creates a view "men"
NOSQL('users').view('men').make(function(builder) {
builder.where('sex', 'male');
builder.sort('age');
});
then insert some documents and it's ready
NOSQL('users').find('women').search('name', 'Anna').callback(function(err, response) {
console.log('All women with name "Anna"', response);
});
You don't need to call nosql.view('women').make(... before every use, only once you start your app