我建议如下结构:
Album {
Id,
UserId,
Title,
Category,
Tags (list of tag names for fast access and for searching),
Ratings (user ids, use $addToSet),
Likes (user ids, use $addToSet),
ViewsCount, (probably just integer value,)
RatingsCount (use $inc to increment this field once someone vote for album),
CommentsCount (use $inc everytime when someone post comment),
LikesCount (use $inc everytime when someone click 'Like it')
}
Comment {
Id,
AlbumId,
Text,
CreatedDate,
Ratings,
RatingsCount,
Replies { (collection of comment replies)
Text,
CreatedDate,
Ratings,
RatingsCount
}
}
Tag {
Id,
TagName,
AlbumsCount (use $inc: 1 when new album created with this tag
and $inc:-1 - once deleted)
}
我已将 cmets 移动到单独的集合中(而不是将其嵌入到相册中),因为目前在 mongodb 中很难更新具有一层以上嵌套的文档。
现在查询:
1) 通过分页获取前 10 个最喜欢的专辑(标题、缩略图、视图、喜欢、作者和日期)。如果用户点击下一个,则获得下一个 10 个最喜欢的专辑等等
db.albums.find().skip(0).limit(10).sort( { RatingsCount: -1 } );
2) 通过分页获取前 10 个观看次数最多的专辑。
db.albums.find().skip(0).limit(10).sort( { ViewsCount: -1 } );
3) 通过分页获取前 10 个讨论最多的专辑。
db.albums.find().skip(0).limit(10).sort( { CommentsCount: -1 } );
4) 获取最近创建的带有分页但最多 100 个相册的相册列表
db.albums.find().skip(0).limit(100).sort( { CreatedDate: -1 } );
5) 获取给定用户的所有专辑(标题、缩略图、视图、喜欢、作者和日期)
db.albums.find({UserId: someUserId})
6) 获取特定专辑的详细信息并通过分页仅显示前 10 个 cmets。 Next 将加载接下来的 10 个 cmets,依此类推。
album = db.albums.find({_id: someAlbumId});
comments = db.comments.find({AlbumId: someAlbumId }).skip(0)
.limit(10).sort( { RatingsCount: -1 ,CreateDate: -1 } );
7) 获取相关专辑列表。关系将通过专辑标签或专辑标题完成
请澄清
8) 按关键字搜索会搜索专辑的标题或标签字段。
db.albums.find( { $or : [ { Title : searchKey } , { Tags : searchKey } ] } )
注意:可能需要存储两次标签:小写用于搜索,原样用于显示
9) 当有人点击一个标签时,获取所有带有该标签的专辑列表
db.albums.find( {Tags : { $in: [tagName1, tagName2]}} ] } )
注意:使用 $in 运算符可以按多个标签名称进行搜索。
10)当有人点击分类链接时,获取10个分类相册的列表
db.albums.find({Category: val }).skip(0).limit(10).sort( { CreatedDate: -1 } );
11) 获取按评级、日期等排序的 cmets
db.comments.skip(0).limit(10).sort( { RatingsCount: -1 } );
12)可以控制文档中新条目的顺序吗?
请澄清
我认为现在您看到您可以将关系数据库迁移到 MongoDB,并相信您的应用程序使用 mongodb 及以上架构会非常快;)。
希望对您有所帮助。
P.S:如果 sql 2005 比我想你使用一些 .net 语言?