[英]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.