1024programmer Mongodb MongoDB study notes (2) realize basic data operation through samus driver

MongoDB study notes (2) realize basic data operation through samus driver

1. About the driver of MongoDB

MongoDB supports drivers in multiple languages, and we only introduce C# drivers here. There are many kinds of C# drivers, and the form of each driver is roughly the same, but the details are different, so the code cannot be used universally. The more commonly used ones are the official driver and the samus driver. In addition to supporting general forms of operations, the samus driver also supports linq to manipulate data. Everyone prefers this way.

Official driver download address: Click to download
Samus driver download address: Click to download

This article will start with the samus driver to explain database access, international practices, and access to “Hello World!”.

2. Realize HelloWorld access through the samus driver

Before performing the following operations, please make sure that the MongoDB service has been started. If you don’t know how to start the service, please read the previous article. Download the driver, create a new console project, and add a reference to MongoDB.dll. If you downloaded the driver source code, just compile and reference the generated DLL.

The basic code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

twenty one

twenty two

twenty three

twenty four

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

// link string

string cOnnectionString=”mongodb://localhost”;

the

//Database name

string databaseName = “myDatabase”;

the

//collection name

string collectiOnName=”myCollection”;

the

//Define the Mongo service

Mongo mOngo=new Mongo(connectionString);

the

//Get the database corresponding to databaseName, if it does not exist, it will be created automatically

MongoDatabase mOngoDatabase= mongo.GetDatabase(databaseName)as MongoDatabase;

the

//Get the collection corresponding to collectionName, if it does not exist, it will be created automatically

MongoCollection mOngoCollection= mongoDatabase.GetCollection(collectionName)as MongoCollection;

the

//link database

mongo. Connect();

try

{

//Define a document object and store two key-value pairsDocument doc =new Document();

doc[“ID”] = 1;

doc[“Msg”] = “Hello World!”;

the

// Insert this document object into the collection

mongoCollection. Insert(doc);

the

//Find the document object whose key-value pair is ID=1 in the collection

Document docFind = mongoCollection. FindOne(new Document { {“ID”, 1 } });

the

//Output the value corresponding to the key “Msg” in the found document object, and output

Console.WriteLine(Convert.ToString(docFind[“Msg”]));

}

finally

{

//Close the link

mongo. Disconnect();

}

Run the program and print helloword successfully. At the same time, we opened the data folder and found two more files “myDatabase.ns” and “myDatabase.0”.

Three. Summary

Code download: http://files.cnblogs.com/lipan/MongoDB_001.rar

This article succinctly explains the basic access operations, and the next article will combine the MVC framework to implement the basic addition, deletion, query, and modification operations of a single collection in the model layer through MongoDB.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/mongodb-study-notes-2-realize-basic-data-operation-through-samus-driver/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索