I’m trying to update objects inside an array based on specific conditions that I check using two functions. I couldn’t achieve this with the match
feature available in mongoose
.
As you can see below, I’m trying to loop over the array of posts, send each post and check if it fulfills a condition, then update it using findOneAndUpdate
. The functions isLessThanOne
and isAchieved
return the expected value.
JavaScript
x
17
17
1
(async () => {
2
3
let posts = await Post.find()
4
5
posts.map(async (post) => {
6
if (isLessThanOneDay(post.deadline)) {
7
console.log(post.id, post._id)
8
await Post.findOneAndUpdate(post.id, { $set: { category: 'deadline' }})
9
} else if (isAchieved(post.milestones)) {
10
await Post.findOneAndUpdate(post.id, { $set: { category: 'achieved' }})
11
} else {
12
await Post.findOneAndUpdate(post.id, { $set: { category: 'newGoals' }})
13
}
14
})
15
16
})()
17
This is the schema
JavaScript
1
110
110
1
const { type } = require("express/lib/response")
2
const mongoose = require("mongoose")
3
4
const PostSchema = new mongoose.Schema({
5
// we have to link or refer each post to a user
6
user: {
7
type: mongoose.Schema.Types.ObjectId,
8
ref: "user",
9
},
10
title: {
11
type: String,
12
required: true
13
},
14
desc: {
15
type: String
16
},
17
milestones: [],
18
likes: [
19
{
20
user: {
21
type: mongoose.Schema.Types.ObjectId,
22
ref: "user"
23
},
24
fName: {
25
type: String,
26
required: true
27
},
28
date: {
29
type: String,
30
required: true
31
}
32
33
}
34
],
35
comments: [
36
{
37
user: {
38
type: mongoose.Schema.Types.ObjectId,
39
ref: "user"
40
},
41
text: {
42
type: String,
43
required: true
44
},
45
fName: {
46
type: String,
47
},
48
date: {
49
type: String,
50
}
51
}
52
],
53
rewards: [
54
{
55
user: {
56
type: mongoose.Schema.Types.ObjectId,
57
ref: "user"
58
},
59
fName: {
60
type: String,
61
required: true
62
},
63
date: {
64
type: String,
65
required: true
66
},
67
reward: {
68
type: String,
69
required: true
70
},
71
price: {
72
type: Number,
73
required: true
74
}
75
}
76
],
77
date: {
78
type: String,
79
required: true
80
},
81
shares: [
82
{
83
user: {
84
type: mongoose.Schema.Types.ObjectId,
85
ref: "user"
86
},
87
fName: {
88
type: String,
89
required: true
90
},
91
date: {
92
type: String,
93
required: true
94
}
95
96
}
97
],
98
deadline: {
99
type: String,
100
required: true
101
},
102
category: {
103
type: String,
104
}
105
106
107
})
108
109
module.exports = Post = mongoose.model('post', PostSchema)
110
The problem is that I couldn’t update the posts and I’ve already tried using udpate
, directly updating it with post.category
then save
. Any help would appreciated.
Advertisement
Answer
Try this:
JavaScript
1
17
17
1
(async () => {
2
3
let posts = await Post.find()
4
5
posts.map(async (post) => {
6
if (isLessThanOneDay(post.deadline)) {
7
console.log(post.id, post._id)
8
await Post.findOneAndUpdate({ _id: post.id }, { $set: { category: 'deadline' }})
9
} else if (isAchieved(post.milestones)) {
10
await Post.findOneAndUpdate({ _id: post.id }, { $set: { category: 'achieved' }})
11
} else {
12
await Post.findOneAndUpdate({ _id: post.id }, { $set: { category: 'newGoals' }})
13
}
14
})
15
16
})()
17
If I recall, the filter parameter must be an object.