Monthly Archives: September 2017

Saving a Twitter stream to MongoDB using C#

In my earlier post I built an example of saving a public Twitter stream into RavenDB. Working with RavenDB has piqued my interest with NoSQL databases so in this post I swap out RavenDB and instead use MongoDB to save some of the tweets that appear in the Twitter Public stream

This example was built using Visual Studio Community 2015 and Mongodb 3.4.6

Start MongoDB (on Windows this is mongod.exe ) then start up an Mongo interactive shell (mongo.exe on Windows).  There is no need to create new database, it will be created by the first insert (As a database developer this took some getting use to!)

Start Visual Studio and create a new Console Application. Using NuGet add Tweetini and MongoDBDriver and then type or copy and paste the following, ensuring that you enter your Twitter API credentials on line 21:

using System;
using Tweetinvi;
using MongoDB.Bson;
using MongoDB.Driver;

namespace SavingTwitterStreamToMongo
{
  class Program
  {
    static void Main(string[] args)
    {
      var connectionString = "mongodb://localhost:27017";

      var client = new MongoClient(connectionString);

      IMongoDatabase db = client.GetDatabase("twitterstream");

      IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("tweets");
      
      // You need to enter your twitter credentials here
      Auth.SetUserCredentials("", "", "", "");

      var stream = Stream.CreateSampleStream();

      stream.TweetReceived += (sender, theTweet) =>
      {
        Console.WriteLine(theTweet.Tweet.FullText);

        var document = new BsonDocument
        {
          { "the_tweet", theTweet.Tweet.FullText }
        };

       collection.InsertOneAsync(document);
     };

     stream.StartStream();
    
     }
   }
 }

This code is the same as shown in this post the only changes are that the RavenDB constructs have been replaced by those required to save the Twitter steam to MongoDB.

Lines 12 and 14 sets up and connects to MongoDB

Line 16 sets the MongoDB database that will be used, in this example it is called twitterstream. (As mentioned earlier in the post, if this database does not exist it will be created on the first insert)

Line 18 shows shows that the tweets are going to be saved inside a collection called tweets.

Line 29 a BSON document is created containing the tweet.

Line 34 saves this document to the database.

Once this code is running, you can switch to the MongoDB interactive shell and review what is being saved.

 

 

 

 

 

 

 

Running the command show dbs you will see the twitterstream database now exists.

 

 

 

 

 

 

 

 

 

 

Switch to the twitterstream database by typing: use twitterstream

 

 

 

Execute the command db.tweets.findOne() and you will see a saved Tweet.