Export MongoDB (edit)

MongoDB & C Sharp: CRUD Operations Tutorial

Using MongoDB in C# - CodeProject

Getting Started MongoDB With C# (c-sharpcorner.com)

Importing and Exporting MongoDB Database (o7planning.org)

mongoexport --db mydb --collection modules --out modules.json
mongoimport --db mydb --collection modules --drop --file modules.json

Compare with PostgreSQL

MongoDB vs PostgreSQL | MongoDB

PostgreSQL vs MongoDB Terminology and Concepts

Many of the terms and concepts used in MongoDB's document model are the same or similar to PostgreSQL's tabular model:

PostgreSQL MongoDB
ACID Transactions ACID Transactions
Table Collection
Row Document
Column Field
Secondary Index Secondary Index
JOINs, UNIONs Embedded documents, $lookup & $graphLookup, $unionWith
Materialized Views On-demand Materialized Views
GROUP_BY Aggregation Pipeline

PostgreSQL JSON operators

PostgreSQL JSON cheatsheet (devhints.io) (HAY HAY HAY)

PowerPoint Presentation (postgresqltutorial.com) (HAY HAY HAY)

PostgreSQL: Documentation: 9.5: JSON Types

PostgreSQL: Documentation: 9.5: JSON Functions and Operators

PostgreSQL JSON Tutorial (postgresqltutorial.com)

PostgreSQL - Data Type (tutorialspoint.com) (all types)

PostgreSQL - Data Types - GeeksforGeeks (HAY HAY HAY)

An Overview of PostgreSQL Data Types | LearnSQL.com (common types)

There are many specialized data types in PostgreSQL. Let’s briefly look at each category of supported data types:

Monetary data types are specialized to store currency amounts with fixed fractional precision. The name of this data type in Postgres is MONEY. Very straightforward!

Binary data types store binary strings. These are not the same as common character strings; a binary string is a sequence of bytes that can store data such as pictures or videos.

Enumerated types store a predefined set of values. In your daily life, you may encounter dropdown menus in different documents or forms. These are nothing but predefined values that might be stored as an enumerated data type.

Geometric types are interesting. They allow you to store two-dimensional geometric figures in your database using points on the coordinate system. A rectangle would be stored using its two diagonal points ((x1,y1),(x2,y2)).

Geometric data types are commonly used with geographical data. Check out our interactive course on POSTGIS to learn more.

According to Google, there’s a question more common than What time is it? That question is What is my IP? Network address types store this information (i.e.  IPv4, IPv6, and MAC addresses) with precision. And it is advisable to use them for their purpose, as they will not allow you to insert an IP address in an incorrect way!

Bit string types store strings consisting of 1’s and 0’s. These types can be used to store bit masks of predefined or varying lengths.

Text search types support full-text search through a collection of documents to find the best match for the query.

The UUID type stores Universally Unique IDentifiers. An algorithm-generated identifier is (almost) guaranteed to be unique in the whole known universe, but only if we consider all the identifiers generated by this specific algorithm.

The XML type stores XML data. The advantage of this type over text data types is that it checks the XML format and provides functions that operate on XML data.

JSON types store JSON data. Like the XML type, they ensure the correctness of the JSON data and provide functions to operate on it.

Sample:

EventId integer,
    EventId bigint,
EventName VARCHAR(50),
    EventName text,
EventStart TIMESTAMP with time zone,
    EventStart DATE,
    EventStart DATE with time zone,
    EventStart TIME,
    EventStart TIME with time zone,
Completed boolean
    true/yes/on/1
    false/no/off/0

Number:

Data type Range Precision
REAL ~ 1E-37 to 1E+37 at least 6 decimal digits
DOUBLE PRECISION ~ 1E-307 to 1E+308 at least 15 digits

Number: There are 3 ways of defining DECIMAL and NUMERIC types:

NUMERIC(precision, scale) DECIMAL(precision, scale)
NUMERIC(precision) DECIMAL (precision)
NUMERIC DECIMAL

Number:

Data type Storage size Range
INTEGER 4 bytes -2147483648 to +2147483647
BIGINT 8 bytes -9223372036854775808 to +9223372036854775807

PostgreSQL Types:

  • Numeric data types
  • Monetary data types
  • Character data types
  • Binary data types
  • Date/Time data types
  • Boolean data types
  • Enumerated data types

Exporting JSON

Exporting JSON (mongodb.github.io)

using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;

// ...

string outputFileName; // initialize to the output file
IMongoCollection collection; // initialize to the collection to read from

using (var streamWriter = new StreamWriter(outputFileName))
{
    await collection.Find(new BsonDocument())
        .ForEachAsync(async (document) =>
        {
            using (var stringWriter = new StringWriter())
            using (var jsonWriter = new JsonWriter(stringWriter))
            {
                var context = BsonSerializationContext.CreateRoot(jsonWriter);
                collection.DocumentSerializer.Serialize(context, document);
                var line = stringWriter.ToString();
                await streamWriter.WriteLineAsync(line);
            }
        });
}