[英]MongoDB how to update a count field with the number of elements that are in an array on the document
我想更新我的Post
model 中的一个字段。帖子 model 如下所示:
const postSchema = new Schema({
title: {
}
likes: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User'}],
upvotes: {
type: Number,
default: 0
}
})
我有一个posts
数据库,我想通过计算他们的likes
(存储的 ID)来update
这些帖子,并将upvotes
update
为文档中的喜欢数量。 有没有办法做到这一点? 我可以使用Mongo shell
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.