Introduction to MongoDb with .NET part 8: searching in arrays and nested documents

Introduction

In the previous post we went through 4 logical operators in MongoDb: $or, $and, $not and $nor. We saw a couple examples of their usage. The syntax for $or, $and and $nor are similar in that we have to provide an array of filters that are linked with the operator whereas the syntax for $not is simpler. You’ll probably not use the $and operator too often as AND conditions can be simplified by chaining together the filters by a comma.

In this post we’ll see how to query arrays and embedded documents.

Read more of this post

Introduction to MongoDb with .NET part 7: logical operators

Introduction

In the previous post we started looking at logical operators in MongoDb. In particular we saw several examples of using the $gt and $lt operators which stand for greater-than and less-than. We don’t use the mathematical symbols ” for this purpose. Instead, operators are used with sub-documents that are manifested as extended JSON documents. We can chain multiple filters separated by a comma which amounts to an AND operation: the city must have a population of at least 50000 and be located in the state of Texas.

In this post we’ll look at the logical operators $and and $or, $not and $nor.

Read more of this post

Introduction to MongoDb with .NET part 6: a return to querying with query operators

Introduction

In the previous post we looked at how to import all documents from a JSON file using the mongoimport tool. We created two collections in the process: restaurants and zipcodes. Both are readily available data sources for testing in MongoDb. They are large enough and offer varied data structures for meaningful queries.

In this post we’ll return to exploring querying techniques in MongoDb. In particular we’ll start looking at so-called query operators that help us construct more complex queries than the ones we’ve seen so far. We’ll first demonstrate the usage of the greater-than and less-than operators.

Have the MongoDb server and client ready in two different console applications if you want to try the examples yourself.

Read more of this post

Introduction to MongoDb with .NET part 5: data import

Introduction

In the previous post we started discussing queries in MongoDb. Specifically we looked at the find and findOne functions. Both can accept a JSON parameter – a JSON document to be exact – to limit the result set returned. find() returns all documents that match the filter or return all documents if there was no filter provided. findOne will always return a single document even if there are more that match the criteria. findOne can be very useful if you’d like to get familiar with a collection by viewing one of its documents. We’ve also quickly looked at two additional functions. The pretty() function produces a better formatted JSON result set on the screen whereas the count() function returns the number of documents within a collection.

In this post we’ll step back a little from querying and instead look at how to import data into a MongoDb database. Specifically we’ll create two real-life collections. We don’t want to keep adding the records ourselves, that’s very tedious. There are at least two different readily available and importable MongoDb collections. The goal is to be able to run meaningful and real-life queries against realistic data sets.

Read more of this post

Introduction to MongoDb with .NET part 4: querying basics with find and findOne

Introduction

In the previous post discussed some details around insertions in MongoDb. We saw that it was quite a painless and straightforward operation. We use the “db” handle, provide the name of the collection where we’d like to insert a document, call the “insert” function on it and pass in a JSON document. All MongoDb documents must have a unique and immutable ID represented by the “_id” field. By default it is of type ObjectId which comes from the BSON specification. We can also provide an ID of integer or string or some other type ourselves but then it is our responsibility that the ID is unique within the collection otherwise we’ll get an exception. Auto-incremented integer IDs are difficult to implement in MongoDb but storing GUID as a string for the ID is a viable option if you’d like to stick to a familiar ID setting strategy from relational databases.

In this post we’ll start looking into querying in MongoDb which will span multiple posts. We’ll mostly see a lot of short examples.

Read more of this post

Extracting information about key pressed in .NET console applications

Console applications let you extract the key(s) pressed by the user using the Console.ReadKey() method. It returns an object of type ConsoleKeyInfo which includes a number of useful properties.

Read more of this post

How to redirect standard error output for a .NET console application

Normally a .NET console application writes its exception messages directly to the console so that the user can view them.

Here’s an example:

static void Main(string[] args)
{
	RunStandardErrorRedirectExample();			
	Console.ReadKey();
}

private static void RunStandardErrorRedirectExample()
{
	try
	{
		double res = Divide(100, 0);
		Console.WriteLine(res);
	}
	catch (Exception ex)
	{
		using (TextWriter errorWriter = Console.Error)
		{
			errorWriter.WriteLine(ex.Message);
		}
	}
}

private static double Divide(int divideWhat, int divideBy)
{
	return divideWhat / divideBy;
}

You’ll get “Attempted to divide by 0” in the console.

However, this is not the only option you have. The standard error output channel can be overridden.

Read more of this post

Introduction to MongoDb with .NET part 3: document insertions

Introduction

In the previous post we successfully installed the latest version of MongoDb on Windows. I think you’ll agree that it was a very simple and painless process. We started exploring the two most important tools of the MongoDb installation folder. Mongo.exe starts the client with which you can interface with the database using commands and queries. Mongod.exe in turn starts the database. We saw a couple of command and query examples such as inserting a new record, searching for one and also deleting one. The default query language of MongoDb is JavaScript and most parameters to the query functions will be in JSON. A JSON query parameter is in fact also a document.

In this post we’ll take a closer look at insertions. Don’t forget to start both mongo.exe and mongod.exe in two separate command prompts if you want to try the examples yourself.

Read more of this post

Raise exception if HTTP response is other than Success

The standard way nowadays to perform a HTTP request in .NET is using the HttpClient object in the System.Net.Http namespace. There are other objects with a similar purpose, such as HttpWebRequest but HttpClient has probably become the most common.

Read more of this post

Introduction to MongoDb with .NET part 2: installation on Windows

Introduction

In the previous post we discussed the basic characteristics of MongoDb. We said that it was a NoSql database that stores its data in binary JSON, i.e. BSON documents. These documents contain collections that are somewhat like tables in relational databases. In theory any data structure that can be represented by JSON can be stored in a BSON documents. There’s no database schema, there are no constraints, you can store any (un)structured object graph in a collection as long as it can be represented in JSON. MongoDb is also highly scalable and queries have a good performance, especially if the database is tuned well. MongoDb also comes with a couple of drawbacks, such as the lack of stored procedures and transactions. You’ll need to weigh all the proc and cons before you decide which data store technology to use in your project.

In this post we’ll first install the MongoDb executables on the local machine. We’ll then see a couple of basic examples of interacting with the database server from the MongoDb client.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

ARCHIVED: Bite-size insight on Cyber Security for the not too technical.