我尝试使用mongoose更新MongoDB文档中一个对象内的嵌套数组。我已经解决了几个类似的问题,似乎只取得了一点成功,然后res.json返回{ acknowledged:false },没有错误。目标是将对象推入reactions对象内的“likes”数组中
这是我正在尝试更新的文档
_id: new ObjectId("63179b818ebed9da5b433ee0"),
thoughtText: "If Everything works out here, we're supposed to get a notification send to another guy by whomsoever leaves a comment or a like on this post.",
topic: 'Testing out the new notification codes for possible errors',
username: 'anotherguy',
userId: '63179a67849b0348e59d4338',
category: 'Secrets',
createdAt: 2022-09-06T19:12:01.345Z,
reactions: [
{
CommentLikeCount: 0,
mentions: 0,
reactionBody: 'Welcome to U-annon anotherGuy, this is your official first reaction on the site',
username: 'marryGold',
_id: new ObjectId("63179cd18ebed9da5b433ee8"),
reactionId: new ObjectId("63179cd18ebed9da5b433ee9"),
createdAt: 2022-09-06T19:17:37.829Z,
likes: []
},
下面是我当前使用updateOne. EDIT更新文档时使用的查询:结构描述。
// thought schema or post schema
const thoughtSchema = new Schema (
{
thoughtText: {
type: String,
required: true,
minlength: 1,
maxlength: 2000
},
topic:{
type: String,
required: true,
minlength: 1,
maxlength: 300
},
createdAt: {
type: Date,
default: Date.now,
get: createdAtVal => moment(createdAtVal).startOf('hour').fromNow()
},
username: {
type: String,
required: true,
},
userId:{
type: String,
required: true
},
category: {
type: String,
required: true,
},
reactions: [reactionSchema],
likes: [likeSchema],
},
{
toJSON: {
virtuals: true,
getters: true,
},
id: false,
}
相似图式与React图式相似,
//reaction schema
const reactionSchema = new Schema (
{
reactionId: {
type: Schema.Types.ObjectId,
default: () => new Types.ObjectId(),
},
reactionBody: {
type: String,
required: true,
},
username: {
type: String,
required: true,
},
userId:{
type: String,
required: true
},
createdAt: {
type: Date,
default: Date.now,
get: createdAtVal => moment(createdAtVal).startOf('second').fromNow()
},
mention: {
type: Object,
required: false
},
likes: [likeSchema],
CommentLikeCount: {
type: Number,
default: 0,
},
mentions: {
type: Number,
default: 0
}
},
{
toJSON: {
virtuals: true,
getters: true
},
id: false,
}
)
这是完整的控制器功能。包括我如何检查是否相同的用户已经喜欢的评论
//Like comments
async likeComments(req, res){
console.log(req.body, req.params)
try {
const post = await Thought.findById(req.params.thoughtId)
const comment = post.reactions.find(x => x.reactionId.toString() === req.params.reactionId)
const liked = comment.likes.find(x => x.username === req.body.username)
if(!liked){
console.log('its open')
const data = await post.updateOne({"reactions.[reaction]._id": req.params.reactionId}, {$addToSet:{"reactions.$.likes": req.body}}, { runValidators: true, new: true })
console.log(data)
res.json(data)
}else{
console.log('already liked')
const data = await post.updateOne({"reactions.[reaction]._id": req.params.reactionId}, {$pull:{"reactions.$.likes": req.body}}, { runValidators: true, new: true } )
res.json(data)
}
} catch (error) {
console.log(error)
}
我已经忙了一整天了,我真的很感激你能帮我。
1条答案
按热度按时间30byixjq1#
在我的应用程序中,用户创建单词列表。用户可以改变每个列表中单词的顺序,他们也可以改变每个列表的顺序。
当用户创建或编辑列表时,单词列表将被更新。
如果已创建单词列表,则该单词列表将添加到现有单词列表中。
如果编辑了单词列表(添加的单词/删除的单词/更改的单词顺序),则将更新现有单词列表。
如果尚未创建单词列表,则应向数据库中添加新文档。
我找到了一个解决方案,但是如果添加了一个新单词列表,它将运行2个findOneAndUpdate查询。如何使用1个查询来实现这一点?我应该简化架构还是将其更改为使用嵌套对象而不是数组?