Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>two general tips: 1.)don't be afraid to duplicate. It is often a good idea to store the same data differently formatted in different collections. </p> <p>2.) if you want to sort and sum up stuff, it helps to keep count fields everywhere. mongodb's atomic update method together with upsert commands make it easy to count up and to add fields to existing documents.</p> <p>The following is most certainly flawed because it's typed from the top of my head. But better bad examples than no examples I thought ;)</p> <pre><code>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 } } </code></pre> <p>add new tweet:</p> <pre><code>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); </code></pre> <p>Get the top ten links sorted by number of tweets they have?</p> <pre><code>db.x.links.find().sort({'totalCount:-1'}).limit(10); </code></pre> <p>Get the most tweeted link for a specific date?</p> <pre><code>db.x.links.find({'$gt':{'daycount.123413453':0}}).sort({'daycount.123413453':-1}).limit(1); //123413453 is the day you're after </code></pre> <p>Get the tweets for a link?</p> <pre><code>db.x.tweets.find({'links': 'http://lin.k'}); </code></pre> <p>Get the ten latests tweets?</p> <pre><code>db.x.tweets.find().sort({'timeTweeted': -1}, -1).limit(10); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload