我是MongoDB的新手,作为练习我正在构建一个从推文中提取链接的应用程序。这个想法是为一个主题获得最多的推文。我很难为这个应用程序设计架构。
我如何:
我很想得到一些意见。
答案 0 :(得分:3)
两个一般提示: 1.)不要害怕复制。将不同格式的相同数据存储在不同的集合中通常是个好主意。
2。)如果你想对东西进行排序和总结,那么在任何地方保持计数字段都是有帮助的。 mongodb的原子更新方法与upsert命令一起使得计算和向现有文档添加字段变得容易。
以下内容肯定是有缺陷的,因为它是从头顶输入的。但是比我想的没有例子更好的坏例子;)
colletion tweets:
{
tweetid: 123,
timeTweeted: 123123234, //exact time in milliseconds
dayInMillis: 123412343, //the day of the tweet kl 00:00:00
text: 'a tweet with a http://lin.k and an http://u.rl',
links: [
'http://lin.k',
'http://u.rl'
],
linkCount: 2
}
collection links:
{
url: 'http://lin.k'
totalCount: 17,
daycounts: {
1232345543354: 5, //key: the day of the tweet kl 00:00:00
1234123423442: 2,
1234354534535: 10
}
}
添加新推文:
db.x.tweets.insert({...}) //simply insert new document with all fields
//for each found link:
var upsert = true;
var toFind = { url: '...'};
var updateObj = {'$inc': {'totalCount': 1, 'daycounts.12342342': 1 } }; //12342342 is the day of the tweet
db.x.links.update(toFind, updateObj, upsert);
按照推文的数量排序前十个链接?
db.x.links.find().sort({'totalCount:-1'}).limit(10);
获取特定日期的推文最多的链接?
db.x.links.find({'$gt':{'daycount.123413453':0}}).sort({'daycount.123413453':-1}).limit(1); //123413453 is the day you're after
获取推文的链接?
db.x.tweets.find({'links': 'http://lin.k'});
获取十个延迟推文?
db.x.tweets.find().sort({'timeTweeted': -1}, -1).limit(10);