-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateNullToObj.js
59 lines (49 loc) · 1.02 KB
/
updateNullToObj.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
const mongoose = require("mongoose");
const agent = require("./db/setup");
const { Schema, model } = mongoose;
(async () => {
const TagSchema = new Schema(
{
name: String,
},
{ timestamps: { createdAt: true, updatedAt: false } }
);
const StorySchema = new Schema(
{
tag: {
type: TagSchema,
default: null,
},
},
{ timestamps: true }
);
const Story = model("Story", StorySchema);
const tag = {
name: "Hot",
};
const [story] = await Promise.all([Story.create({})]);
console.log("Created:", JSON.stringify(story, null, 2));
await Promise.all([
Story.updateOne(
{
_id: story._id,
},
{
tag,
}
),
]);
let results = await Story.find({}).lean().exec();
console.log("Updated:", 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);
}
);