Recently a friend of mine asked to me where he can store some data coming from several devices and usable from everywhere.
My first think is to create a REST service which write data into an Azure SQL Server Database… but another requirement of my friend is… I’ve no money… It must be FREE.
Ehm…
No… Azure is cheap but it is not free.
Then I thought to use a NOSQL Database, like DocumentDB or MongoDB.
From Wikipedia…
A NoSQL (originally referring to "non SQL", "non relational" or "not only SQL") database provides a mechanism for storage and retrieval of data which is modeled in means other than the tabular relations used in relational databases. Such databases have existed since the late 1960s, but did not obtain the "NoSQL" moniker until a surge of popularity in the early twenty-first century, triggered by the needs of Web 2.0 companies such as Facebook, Google, and Amazon.com. NoSQL databases are increasingly used in big data and real-time web applications. NoSQL systems are also sometimes called "Not only SQL" to emphasize that they may support SQL-like query languages.
Googling, I found a provider which give you for free up to 500 Mbyte MongoDB database:
https://mlab.com/
After the Sign In operations, I created:
- Database
- Collection
- User (used by devices)
- Index
I added the following Nuget Packages
And then I tried to write some code snipped to perform operation inside this database: a lot of example found on the network works using string JSON entity but, being I an old rippled, I prefer to create classes and typing my code.
My entity is Pass and is defined like this:
class Pass
And here some example to work with data: you can find some useful information in the code comments.class Pass
{
public ObjectId _id { get; set; }
public Guid PassId { get; set; }
public string Place { get; set; }
public string Pectoral { get; set; }
public DateTime Time { get; set; }
public override string ToString()
{
return $"PassId: {PassId.ToString()}, Place: {Place}, Pectoral: {Pectoral}, Time: {Time.ToString("u")}";
}
}
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MongoDB
{
class Pass
{
public ObjectId _id { get; set; }
public Guid PassId { get; set; }
public string Place { get; set; }
public string Pectoral { get; set; }
public DateTime Time { get; set; }
public override string ToString()
{
return $"PassId: {PassId.ToString()}, Place: {Place}, Pectoral: {Pectoral}, Time: {Time.ToString("u")}";
}
}
class Program
{
static void Main(string[] args)
{
//Connection String: you can retrieve the connection string from the mlab portal inside the Users section
///https://mlab.com/databases/dbname#users
//The name of the database
const string DBNAME = "test001";
//The name of the collection
const string COLL = "Collection02";
//Creating client using the connection string
var client = new MongoClient(CONNSTRING);
//Getting Database
var db = client.GetDatabase(DBNAME);
//Getting the Collection
var coll = db.GetCollectionPass>(COLL);
//Calling some example functions
Console.WriteLine(getCount(coll).ToString());
Console.WriteLine("Inserting 1 item");
insert(coll);
Console.WriteLine("Inserting many items");
insertMany(coll);
Console.WriteLine("Getting items");
var passes = getWithFiltersAndSort(coll);
passes.ForEach(pa => Console.WriteLine(pa.ToString()));
Console.WriteLine("Getting first item");
var pass = getFirst(coll);
Console.WriteLine(pass.ToString());
Console.WriteLine("Updating item");
pass.Time = DateTime.UtcNow;
update(coll, pass);
Console.WriteLine("Deleting item");
delete(coll, pass);
Console.WriteLine(getCount(coll).ToString());
Console.ReadLine();
}
///
/// Returns the number of items inside the collection
///
/// coll">
///
private static Int64 getCount(IMongoCollectionPass> coll)
{
return coll.Count(FilterDefinitionPass>.Empty);
}
///
/// Delete the entity specified by a PassId value
///
/// coll">
/// pass">
private static void delete(IMongoCollectionPass> coll, Pass pass)
{
coll.DeleteOne(BuildersPass>.Filter.Eq(pa => pa.PassId, pass.PassId));
}
///
/// Update an entity using the new passed Object
///
/// coll">
/// pass">
private static void update(IMongoCollectionPass> coll, Pass pass)
{
coll.ReplaceOne(BuildersPass>.Filter.Eq(pa => pa.PassId, pass.PassId), pass);
}
///
/// Return the first item of the collection
///
/// coll">
///
private static Pass getFirst(IMongoCollectionPass> coll)
{
return coll.Find(FilterDefinitionPass>.Empty).FirstOrDefault();
}
///