sql >> Base de Datos >  >> NoSQL >> MongoDB

¿Cómo diseño un esquema MongoDB para un agregador de artículos de Twitter?

dos consejos generales:1.) no tenga miedo de duplicar. A menudo es una buena idea almacenar los mismos datos con diferentes formatos en diferentes colecciones.

2.) si desea ordenar y resumir cosas, es útil contar campos en todas partes. El método de actualización atómica de mongodb junto con los comandos upsert facilitan el conteo y la adición de campos a los documentos existentes.

Lo siguiente es ciertamente defectuoso porque está escrito desde la parte superior de mi cabeza. Pero mejor malos ejemplos que ningún ejemplo, pensé;)

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
   }
}

añadir nuevo tuit:

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);

¿Obtener los diez enlaces principales ordenados por número de tweets que tienen?

db.x.links.find().sort({'totalCount:-1'}).limit(10);

¿Obtener el enlace más tuiteado para una fecha específica?

db.x.links.find({'$gt':{'daycount.123413453':0}}).sort({'daycount.123413453':-1}).limit(1); //123413453 is the day you're after

¿Obtener los tweets para un enlace?

db.x.tweets.find({'links': 'http://lin.k'});

¿Obtener los diez últimos tweets?

db.x.tweets.find().sort({'timeTweeted': -1}, -1).limit(10);