我有2个收藏:曲目和用户
简化为:
trackSchema = new mongoose.Schema
title: String
artist: String
userId: String
playlists: [String]
userSchema = new mongoose.Schema
username: String
trackCount: Integer
playlists: {}
因此,user.playlists会跟踪每个播放列表中的曲目数量,而trackCount会跟踪用户的曲目总数
假设user1有2个音轨,track1和track2。两者都属于1个名为myplaylist的播放列表,我会有
track1.playlists = ["myplaylist"]
track2.playlists = ["myplaylist"]
user1.playlists = { myplaylist: 2 }
user1.trackCount = 2
以这种方式跟踪计数器是否有用?我发现它使实现变得复杂,但我确保Mongo不是每次需要时都查询计数,这有意义吗?