forked from annette-arrigucci/mongoose-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-methods-mongoose.js
65 lines (59 loc) · 3.03 KB
/
add-methods-mongoose.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
//console.log("We're connected!");
var personSchema = mongoose.Schema({
firstName: String,
lastName: String,
age: Number,
email: String,
phone: String,
address: String
});
//add custom document instance method
personSchema.methods.getName = function() {
return this.firstName + " " + this.lastName;
}
//return a string representation of object
personSchema.methods.printPerson = function() {
return "First name: " + this.firstName +
", Last name: " + this.lastName +
", Age: " + this.age +
", Email: " + this.email +
", Phone: " + this.phone +
", Address: " + this.address
}
var Person = mongoose.model('Person', personSchema);
//insert one object
var annette = new Person({firstName: "Annette", lastName: "Arrigucci", age: "35", email: "[email protected]", phone: "555-555-5555", address: "500 Broad Street"});
annette.save(function (err) {
if (err) return handleError(err);
// saved!
});
/*
//insert many objects into collection
//these are raw objects
var peopleArray = [
{ firstName: "John", lastName: "Doe", age: "21", email: "[email protected]", phone:"111-111-1111", address: "Highway 71" },
{ firstName: "Peter", lastName: "Smith", age: "37", email: "[email protected]", phone:"222-222-2222", address: "Lowstreet 4"},
{ firstName: "Amy", lastName: "Lopez", age: "23", email: "[email protected]", phone:"333-333-3333", address: "Apple St 652" },
{ firstName: "Hannah", lastName: "Thomas", age: "24", email: "[email protected]", phone:"444-444-4444", address: "Mountain 21" },
{ firstName: "Michael", lastName: "Parker", age: "25", email: "[email protected]", phone:"555-555-5555", address: "Valley 345" },
{ firstName: "Sandy", lastName: "Mitchell", age: "26", email: "[email protected]", phone:"666-666-6666", address: "Ocean Blvd 2" },
{ firstName: "Betty", lastName: "Brown", age: "44", email: "[email protected]", phone:"777-777-7777", address: "Green Grass 1" },
{ firstName: "Richard", lastName: "Jones", age: "28", email: "[email protected]", phone:"888-888-8888", address: "Sky 331" },
{ firstName: "Susan", lastName: "Kennedy", age: "77", email: "[email protected]", phone:"999-999-9999", address: "Apple St 670" },
{ firstName: "Vicky", lastName: "Conoors", age: "55", email: "[email protected]", phone:"101-101-1010", address: "Mountain 84" }
];
Person.insertMany(peopleArray, function(error, docs) {
if (error) return handleError(error);
});
*/
//try out the instance methods I added
console.log("Get name of object: "+ annette.getName());
console.log("Get string representation of object: " + annette.printPerson());
});