Examples (ODM)
Define a User model
var User = db.define('User');
User.property('name');
User.property('address');
User.property('age', Number);
User.property('married', 'boolean');
User.property('email', 'string');
User.index('age', function(user) {
return user.age;
});
Create a new User document
var user = User.new(USER);
user.save(function(validationErrors) {
if (validationErrors) {
console.log('Did not save because of some validation errors:');
console.log(validationErrors);
} else {
console.log('User saved');
}
});
Find User document by ID
var id = 'abc';
User.get(id, function(user) {
console.log('found user: ' + user.inspect());
});
Find
User.find({age: {$lt: 30}}) (function(gotUser) {
console.log('Found user ' + gotUser.inspect() + ' that has less than 30 years old');
}).all(function(users) {
console.log('Found ' + users.length + ' users that are less than 30 years old');
}).where({age: {$gt: 30}}).all(function(users) {
console.log('Found ' + users.length + ' users that are more than 30 years old');
});