forked from anurag1209/learnMongooseBasics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
163 lines (119 loc) · 3.73 KB
/
app.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var mongoose = require('mongoose');
mongoose.connect('mongodb://mongouser:[email protected]:33964/learnmongo');
var personSchema = mongoose.Schema({
name : String,
age : Number,
nickname : String
});
// Attaching methods to a schema
personSchema.methods.tellName = function () {
var greeting = this.name
? "My name is " + this.name
: "I dont have any name";
console.log(greeting);
}
var Person = mongoose.model('Person', personSchema);
// ----------------------------------------------------------------------
// Part-1 : Inserting Data into DB
var Anurag = new Person({
name : "Anurag",
age : 27,
nickname : "Lallu"
});
Anurag.tellName();
Anurag.save(function(err, data){
if(err){
throw err;
}
console.log("Name " + data.name + " saved in database");
});
// ------------------------------------------------------------------------
// Part-2 : Finding Data from DB
Person.find({}, function(err, data){
if(err){
throw err;
}
console.log(data);
});
Person.find({name : "Anurag"}, function(err, data){
if(err){
throw err;
}
console.log(data);
});
// ------------------------------------------------------------------------
// Part-3 : Updating Data into Document
Person.update({"name" : "Shubham"}, {$set : {'nickname' : 'Kaaliya'}}, function(err, data){
if(err){
throw err;
}
console.log(data);
});
// ------------------------------------------------------------------------
// Part-4 : Deleting Document from DB
Person.remove({'name' : 'Anurag'}, function(err, data){
if(err){
throw err;
}
console.log(data);
console.log("Requested data is removed");
});
// ------------------------------------------------------------------------
// Part-5 : "OR" condition in mongoDB
Person.find({$or : [{'name' : 'Anurag'}, {'age' : 26}]}, function(err, data){
console.log(data);
});
// ------------------------------------------------------------------------
// Part-6 : "AND" condition in mongoDB
Person.find({$and : [{'name' : 'Anurag'}, {'age' : 24}]}, function(err, data){
console.log(data);
});
// ------------------------------------------------------------------------
// Part-7 : use of "AND" & "OR" condition in combined mongoDB
Person.find({ $or : [{ $and : [{'name' : 'Anurag'}, {'age' : 27}] }, { $and : [{'name' : 'Pooja'}, {'age' : 26}]}] }, function(err, data){
if(err){
throw err;
}
console.log(data);
});
// -----------------------------------------------------------------------------
// Part-8 : Getting document's selected values (Projection)
// 1- in projection, we set flags for things we want to retrieve
// 2- 'limit' is used to get the number of posts
// 3- 'skip' is used for skipping number of documents
Person.find({}, {"name":1, "age":1, "_id": 0}, function(err, data){
if(err){
throw err;
}
console.log(data);
}).limit(2);
Person.find({}, {"name":1, "age":1, "_id": 0}, function(err, data){
if(err){
throw err;
}
console.log(data);
}).limit(2).skip(1);
// --------------------------------------------------------------------
// Part-9 : Sort documents (Use sort())... -1 for desc and 1 for asc
Person.find({}, function(err, data){
if(err){
throw err;
}
console.log(data);
}).sort({'name' : -1});
// document inside another document
// openingTimeSchemaModel is defined and included as subdocument inside locationSchemaModel
var openingTimeSchemaModel = new Schema({
days: { type: String, required: true },
opening: String,
closing: String,
closed: { type: Boolean, required: true }
})
var locationSchemaModel = new Schema({
name: { type: String, required: true },
address: String,
rating: { type: Number, default: 0, min: 0, max: 5 },
facilities: [String],
coords: { type: [Number], index: '2dsphere' },
openingTimes: [openingTimeSchemaModel]
})