- 전체
- Sample DB
- database modeling
- [표준 SQL] Standard SQL
- G-SQL
- 10-Min
- ORACLE
- MS SQLserver
- MySQL
- SQLite
- postgreSQL
- 데이터아키텍처전문가 - 국가공인자격
- 데이터 분석 전문가 [ADP]
- [국가공인] SQL 개발자/전문가
- NoSQL
- hadoop
- hadoop eco system
- big data (빅데이터)
- stat(통계) R 언어
- XML DB & XQuery
- spark
- DataBase Tool
- 데이터분석 & 데이터사이언스
- Engineer Quality Management
- [기계학습] machine learning
- 데이터 수집 및 전처리
- 국가기술자격 빅데이터분석기사
- 암호화폐 (비트코인, cryptocurrency, bitcoin)
NoSQL How to work with MongoDB in .NET
2020.09.30 21:31
How to work with MongoDB in .NET
Get started with documents and collections in the popular, open source, NoSQL database using C#
MongoDB is a popular, open source, scale-out NoSQL database that provides high throughput for your data-driven applications. Unlike relational databases such as SQL Server, Oracle, and MySQL, which store data in tables according to a rigid schema, MongoDB stores data in documents with flexible schema. There are many such non-relational databases around including CouchDB, RavenDB, and Couchbase. However, I like MongoDB primarily due to its scalability, speed, and dynamic querying capabilities.
MongoDB uses the BSON format under the hood to represent the JSON documents at the heart of the data store. BSON or “Binary JSON” is a lightweight and efficient binary-encoded data serialization format that supports fast data traversal and searches. BSON also allows MongoDB to support data types—namely int, long, date, floating point, and decimal128—not represented in JSON.
In MongoDB documents are part of collections, in much the same way as a row is part of a table in a relational database. A document is essentially a collection of field and value pairs, which can also be nested. Note that a value in MongoDB can be a document, an array of documents, an array of BSON, or just a BSON type. Let’s look at how we can work with MongoDB using C#.
Install MongoDB and create a new project
Start by downloading the MongoDB binaries. Unzip the binaries to a folder of your choice in your system and create a separate folder (in my case C:\data\db) for the data. Then, to start MongoDB, navigate to the folder where MongoDB is installed and execute the mongod
command in the command prompt window. That should start MongoDB at port 27017 by default.
Create a new console application project in Visual Studio and install the MongoDB.Driver package via the NuGet Package Manager Console with the following command.
PM> Install-Package MongoDB.Driver
This will install the following three NuGet packages at one go.
- MongoDB.Bson
- MongoDB.Driver.Core
- MongoDB.Driver
Connect to your MongoDB instance
To connect to a MongoDB instance at its default port 27017, you can use the default constructor of the MongoClient
class as shown below.
Now consider the following class. We will use this class to store data in MongoDB.
Create a database and collection
The following code listing shows how you can create a database and a collection inside it and then insert an object inside the collection.
Note that the following namespaces should be included in your program.
Now refer to the Main
method in the code listing above. Note that the following statement creates new a database named ”IDG”
if none exists by that name.
IMongoDatabase db = client.GetDatabase(“IDG”);
Similarly, the following statement creates a new collection of ”Author”
objects if none exists. In either case, the GetCollection
method returns a collection instance.
Add documents to the collection
Next, we create an instance of the Author
class and assign values to its FirstName
and LastName
properties.
Use the statement below to insert the instance of the Author
class into the collection.
Note that you can insert multiple documents all at the same time using the InsertMany
or InsertManyAsync
method. The following code listing illustrates how the InsertMany
method can be used.
The BsonDocument
class in the MongoDB.Bson package is used to represent a BSON document. The following code snippet shows how you can display the names of the databases available in the instance of MongoDB running in your system.
When you execute the above code snippet, you will see the name of the database (i.e., “IDG”
) listed in the console window. You can also use the asynchronous method, ListDatabasesAsync
, to list the database names, as shown in the code snippet given below.
MongoDB is a popular NoSQL database that has a flexible data model and scales gracefully. MongoDB provides support for horizontal scalability using a technique known as sharding. I will discuss more advanced concepts in MongoDB in future posts here. Until then, you may want to read up on the MongoDB C# driver in the MongoDB documentation.
[출처] https://www.infoworld.com/article/3224457/how-to-work-with-mongodb-in-dotnet.html
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.