I can't seem to increment a nested value in mongoose. I have tried multiple revisions that I have found online and none seem to work. I am using mongodb version 3.2 and mongoose 4.9.5.
This project is a forum. There is a thread with a "posts" property which is an array. Each post has a "like" property which has a "users" property for each person who liked that post, and a count property that should increment with each update. Here is my model:
const threadSchema = mongoose.Schema({
title: {type: String, required: true},
date: {type: Date, default: Date.now},
author: {type: String, required: true},
content: {type: String},
posts: [{
content: {type: String, required: true},
user: {type: String, required: true},
created: {type: Date, default: Date.now},
likes: {
users: [String],
count: {type: Number, default: 0}
},
comments: [{
comment: {type: String},
user: {type: String},
created: {type: Date, default: Date.now},
likes: {type: Number, default: 0}
}]
}]
});
And here is my update code. It is pushing to the posts.likes.users array just fine but wont increment:
Threads.findOneAndUpdate({"posts._id": req.body.postId}, {"$push": {"posts.$.likes.users": user}}, {$inc: {"posts.$.likes.count": 1}})
I have also tried these versions:
{"$inc": {"posts.likes.count": 1}}
{"$inc": {"posts.$.likes.count": 1}}
{"$inc": {"posts.$.likes.$.count": 1}}
Thanks.
via DGwang
No comments:
Post a Comment