Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to install and use MongoDb in C #

Shulou Source: shulou.com Published: 2022-06-02 05:18:53 10月05日 Update

This article mainly explains "how to install and use MongoDb in C #". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to install and use MongoDb in C #".

MongoDb visualization tool

I use Robomongo, which feels good to use and can be downloaded by myself.

C # operation

Create a new WinForm program, then right-click in the reference and select manage NuGet package.

Enter MongoDb in browsing to search

Install the first MongoDb.Driver, and then you will have the corresponding DLL in the reference.

Note: it is found that MongoServer cannot be used in the course of use, so it is necessary to install mongocsharpdriver.

Add a TextBox and a Button to the form interface

Then we create a new Class named CPersonal, which corresponds to the Personal of our MyDemo database in Mongodb.

To establish a database connection, you must know the address, port and other information of the server. All of this information is represented by a connection string. The connection string format for MongoDB is as follows:

Mongodb:// [username:password@] host1 [: port1] [, host2 [: port2], … [, hostN [: portN] [/ [database] [? options]]

Let's take a look at the meaning of each field in the connection string:

Mongodb://: this is the prefix of the MongoDB connection string

Username:password (Optional): optional, indicating login username and password, used to complete user security authentication

HostN: must specify at least one host, indicating the MongoDB instance to which you are connected

PortN (Optional): optional, connect to 27017 by default

Database (Optional): if you specify a username:password@, connection and verify login to the specified database. If not specified, the admin database is opened by default.

Options (Optional): optional, if you don't use / database, you need to precede it with /. All connection options are key-value pairs name=value, separated by & or; (semicolon)

We write the events of button1 to query all the information in Personal.

Var list = collection.AsQueryable ()

Foreach (CPersonal item in list)

{

TextBox1.AppendText ("name:" + item.name + "age:" + item.age)

TextBox1.AppendText ("\ r\ n")

}

Get all the Personal data through collection.AsQueryable, then output it with foreach, and click the button effect after running it.

If you look for people older than 30, you can use var list = collection.AsQueryable () above, followed by the lamda expression of where.

Var list = collection.AsQueryable () .where (t = > t.age > 30)

The effect is as follows

The method of changing to non-Lamda expression

Var list = from t in collection.AsQueryable ()

Where t.age > 30

Select t

At this point, I believe you have a deeper understanding of "how to install and use MongoDb in C #". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Data characters strings database caching information content age effect method user program expression learning query validation good practical deeper between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Tech Info Shulou Technology Huawei MariaDB