-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubdocument.js
124 lines (108 loc) · 3.08 KB
/
subdocument.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
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
username: {
type: String,
required: true
},
fullName: {
type: String,
required: true
},
addresses: [{
street: String,
city: String,
likes: { type: Number, default: 0 },
state: {
type: String,
enum: ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT']
},
zip: Number
}]
});
const User = mongoose.model('User', UserSchema);
mongoose.connect('mongodb://localhost:27017/demo-books', { useMongoClient: true })
.then(() => {
return mongoose.connection.db.dropDatabase();
})
// *****************************
// Create a new User
// *****************************
.then(() => {
return User.create([
{
_id: 'aaaaaaaaaaaaaaaaaaaaaaaa', username: 'aalabaster', fullName: 'Alice Alabaster',
addresses: [
{ street: '123 Main St.', city: 'Springfield', state: 'CO', zip: 12345 }]
},
{ _id: 'bbbbbbbbbbbbbbbbbbbbbbbb', username: 'bbaker', fullName: 'Bob Baker', addresses: [{ street: '123 Front St.', city: 'Downtown', state: 'CA', zip: 12345 }] },
{ _id: 'cccccccccccccccccccccccc', username: 'cconway', fullName: 'Chuck Conway' }
]);
}).then(results => {
console.log('create new item: \n', results);
})
// *****************************
// $push a new item onto an array
// using .findByIdAndUpdate()
// *****************************
.then(() => {
return User
.findByIdAndUpdate(
'aaaaaaaaaaaaaaaaaaaaaaaa',
{
'$push': {
addresses: { street: '789 Cactus St.', city: 'Scottsdale', state: 'AZ', zip: 56789 }
}
},
{ new: true }
);
}).then(results => {
console.log('$push results: \n', results);
})
// *****************************
// Update a single item in the array using the $ placeholder
// using findOneAndUpdate()
// *****************************
.then(() => {
return User
.findOneAndUpdate(
{ username: 'aalabaster', 'addresses.state': 'AZ' },
{ 'addresses.$.city': 'Phoenix' },
{ new: true }
);
}).then(results => {
console.log('update', results);
})
// *****************************
// $inc a property in an array
// findOneAndUpdate()
// *****************************
.then(() => {
return User
.findOneAndUpdate(
{ username: 'aalabaster', 'addresses.state': 'AZ' },
{ $inc: { 'addresses.$.likes': 1 } },
{ new: true }
);
}).then(results => {
console.log('update', results);
})
// *****************************
// $pull (remove) an item from an Array
// findOneAndUpdate()
// *****************************
.then(() => {
return User
.findByIdAndUpdate(
'aaaaaaaaaaaaaaaaaaaaaaaa',
{ $pull: { addresses: { city: 'Springfield' } } },
{ new: true }
);
}).then(results => {
console.log('remove', results);
})
.then(() => {
return mongoose.disconnect();
}).catch((err) => {
console.error(err);
return mongoose.disconnect();
});