-
Notifications
You must be signed in to change notification settings - Fork 0
/
pullPushTest.js
115 lines (100 loc) · 2.26 KB
/
pullPushTest.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
// const {
// Worker,
// isMainThread,
// parentPort,
// workerData,
// } = require('worker_threads');
const mongoose = require('mongoose');
const agent = require('./db/setup');
const { Schema, model } = mongoose;
(async () => {
const PenNameGroupSchema = new Schema(
{
_id: {
type: mongoose.Schema.Types.ObjectId,
required: true,
},
},
{ _id: false, timestamps: { createdAt: true, updatedAt: false } }
);
const PenNameSchema = new Schema(
{
penNameGroups: {
type: [PenNameGroupSchema],
default: [],
},
},
{ timestamps: true }
);
const PenName = model('PenName', PenNameSchema);
const penNameGroups = [
{
_id: new mongoose.Types.ObjectId(),
},
{
_id: new mongoose.Types.ObjectId(),
},
{
_id: new mongoose.Types.ObjectId(),
},
];
const [penName] = await Promise.all([
PenName.create({
penNameGroups,
}),
]);
let results = await PenName.find({}).lean().exec();
console.log('Created:', JSON.stringify(results, null, 2));
const newPenNameGroups = [
{
_id: new mongoose.Types.ObjectId(),
},
{
_id: new mongoose.Types.ObjectId(),
},
];
const newPenNameGroupIds = newPenNameGroups.map((_) => _._id.toString());
await new Promise((_) => setTimeout(_, 1000));
await PenName.findByIdAndUpdate(
penName._id,
{
$push: {
penNameGroups: {
$each: newPenNameGroupIds.map((_id) => ({ _id })),
},
},
},
{
new: true,
}
);
results = await PenName.find({}).lean().exec();
console.log('Updated:', JSON.stringify(results, null, 2));
const deletedPenNameGroupIds = [
penNameGroups[0],
penNameGroups[2],
newPenNameGroups[1],
].map((_) => _._id.toString());
await PenName.findByIdAndUpdate(penName._id, {
$pull: {
penNameGroups: {
_id: {
$in: deletedPenNameGroupIds,
},
},
},
});
results = await PenName.find({}).lean().exec();
console.log('Deleted:', JSON.stringify(results, null, 2));
return results;
})().then(
async (results) => {
await agent.close();
process.exit(0);
},
async (_) => {
console.log(_);
await agent.close();
process.exit(1);
}
);