-
Notifications
You must be signed in to change notification settings - Fork 0
/
cats.js
55 lines (44 loc) · 999 Bytes
/
cats.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
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/cats",{ useNewUrlParser: true, useUnifiedTopology: true });
var catSchema = new mongoose.Schema({
name: String,
age: Number,
});
var Cat = mongoose.model("cat",catSchema);
//ADDING A CAT
/* var catInfo = new Cat({
name: "Norris",
age: 9
});
//add cats to the database
catInfo.save((err,cat) => {
if(err){
console.log("Error occured!");
}
else{
console.log(cat);
}
}); */
//ANOTHER WAY OF ADDING A CAT
/* CREATING A CAT AND ADDING IT TO THE DATABASE CAN ALSO BE DONE BY CREATE METHOD AS FOLLOWS:
Cat.create({
name: "catname",
age: 7
},function(err,cat){
if(err){
console.log("Error occured!");
}
else{
console.log("Cat added!");
}
});
*/
//QUERING THE DATABASE TO FIND ALL THE CATS
Cat.find({},function(err,cats){
if(err){
console.log("Error occured" + err);
}
else{
console.log(cats);
}
});