on
Italy
- Get link
- X
- Other Apps
$ npm install mongoose --save
var mongoose = require('mongoose');
We also have to connect to a MongoDB database (either local or hosted):mongoose.connect('mongodb://localhost/myappdatabase');
// grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// create a schema
var userSchema = new Schema({
name: String,
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
admin: Boolean,
location: String,
meta: {
age: Number,
website: String
},
created_at: Date,
updated_at: Date
});
// the schema is useless so far
// we need to create a model using it
var User = mongoose.model('User', userSchema);
// make this available to our users in our Node applications
module.exports = User;
mongoose and mongoose.Schema. Then we can define our attributes on our userSchema for all the things we need for our user profiles. Also notice how we can define nested objects as in the meta attribute.SchemaTypes are:mongoose.model. We can also do more with this like creating specific methods. This is a good place to create a method to hash a password.// grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// create a schema
var userSchema ...
// custom method to add string to end of name
// you can create more important methods like name validations or formatting
// you can also do queries and find similar users
userSchema.methods.dudify = function() {
// add some stuff to the users name
this.name = this.name + '-dude';
return this.name;
};
// the schema is useless so far
// we need to create a model using it
var User = mongoose.model('User', userSchema);
// make this available to our users in our Node applications
module.exports = User;
// if our user.js file is at app/models/user.js
var User = require('./app/models/user');
// create a new user called chris
var chris = new User({
name: 'Chris',
username: 'sevilayha',
password: 'password'
});
// call the custom method. this will just add -dude to his name
// user will now be Chris-dude
chris.dudify(function(err, name) {
if (err) throw err;
console.log('Your new name is ' + name);
});
// call the built-in save method to save to the database
chris.save(function(err) {
if (err) throw err;
console.log('User saved successfully!');
});
created_at variable to know when the record was created. We can use the Schema pre method to have operations happen before an object is saved.created_at if this is the first save, and to updated_at on every save:// on every save, add the date
userSchema.pre('save', function(next) {
// get the current date
var currentDate = new Date();
// change the updated_at field to current date
this.updated_at = currentDate;
// if created_at doesn't exist, add to that field
if (!this.created_at)
this.created_at = currentDate;
next();
});
Now on every save, we will add our dates. This is also a great place
to hash passwords to be sure that we never save plaintext passwords.User method we created earlier. The built-in save method on mongoose Models is what is used to create a user:// grab the user model
var User = require('./app/models/user');
// create a new user
var newUser = User({
name: 'Peter Quill',
username: 'starlord55',
password: 'password',
admin: true
});
// save the user
newUser.save(function(err) {
if (err) throw err;
console.log('User created!');
});
// get all the users
User.find({}, function(err, users) {
if (err) throw err;
// object of all the users
console.log(users);
});
// get the user starlord55
User.find({ username: 'starlord55' }, function(err, user) {
if (err) throw err;
// object of the user
console.log(user);
});
// get a user with ID of 1
User.findById(1, function(err, user) {
if (err) throw err;
// show the one user
console.log(user);
});
// get any admin that was created in the past month
// get the date 1 month ago
var monthAgo = new Date();
monthAgo.setMonth(monthAgo.getMonth() - 1);
User.find({ admin: true }).where('created_at').gt(monthAgo).exec(function(err, users) {
if (err) throw err;
// show the admins in the past month
console.log(users);
});
// get a user with ID of 1
User.findById(1, function(err, user) {
if (err) throw err;
// change the users location
user.location = 'uk';
// save the user
user.save(function(err) {
if (err) throw err;
console.log('User successfully updated!');
});
});
Remember that since we created the function to change the updated_at date, this will also happen on save.// find the user starlord55
// update him to starlord 88
User.findOneAndUpdate({ username: 'starlord55' }, { username: 'starlord88' }, function(err, user) {
if (err) throw err;
// we have the updated user returned to us
console.log(user);
});
// find the user with id 4
// update username to starlord 88
User.findByIdAndUpdate(4, { username: 'starlord88' }, function(err, user) {
if (err) throw err;
// we have the updated user returned to us
console.log(user);
});
// get the user starlord55
User.find({ username: 'starlord55' }, function(err, user) {
if (err) throw err;
// delete him
user.remove(function(err) {
if (err) throw err;
console.log('User successfully deleted!');
});
});
// find the user with id 4
User.findOneAndRemove({ username: 'starlord55' }, function(err) {
if (err) throw err;
// we have deleted the user
console.log('User deleted!');
});
// find the user with id 4
User.findByIdAndRemove(4, function(err) {
if (err) throw err;
// we have deleted the user
console.log('User deleted!');
});
Comments
Post a Comment