Use C # to query and modify MongoDB data

Source: Internet
Author: User

First use the official C # access component https://github.com/mongodb/mongo-csharp-driver
Then, reference MongoDB after compilation. bson. dll and MongoDB. driver. dll, and declare reference to using MongoDB In the cs file. bson; using MongoDB. driver; using MongoDB. driver. builders; the first is the namespace in Bson format, the second is the primary space, and the third is the constructor namespace used to construct query and update conditions.
1. Database connection login client = null; Login server = null; // connection string conStr = "mongodb: // user: pw@127.0.0.1/db1"; client = new Login client (conStr ); server = client. getServer (); server. connect ();
Pay attention to the writing method of the connection string, the above writing method is with user authentication, more information about the writing method of the connection string can refer to the http://docs.mongodb.org/ecosystem/tutorial/authenticate-with-csharp-driver/
2. Obtain the Collection objects database db = server in the database. getDatabase ("db1"); Response collection colaa = db. getCollection ("col1"); get the Collection named col1 in the db1 database,
The generic collection obtained here supports generics. It can be obtained in the format of one row by default BsonDocument, or by a custom class. If it is obtained by a line by BsonDocument, the Code is as follows (each BsonDocument object is a Collection line): Export cursor <BsonDocument> doc = colaa. findAllAs <BsonDocument> (); foreach (BsonDocument book in doc ){}
If a row is obtained based on a custom class, the Code is as follows: Export cursor <row> res = colaa. findAllAs <row> (); foreach (row row1 in res) {} pay attention to the fact that, if you get it by custom class, the class must be pre-defined, in addition, the variable names in the class must be consistent with those in the database, and the number of columns must also be consistent. If a column in the database is displayed but this object is missing in the class, an error is reported, the following is an example of a class definition: public class row {public ObjectId _ id; public string name; public string part; public string age;} in the above example, the FindAllAs function is for all queries, there are also some other row query and filtering methods, see rows
If it is obtained by BsonDocument, each row of Data corresponds to a BsonDocument. A BsonDocument consists of multiple "Name-Value" pairs, where Name is in String format, the Value is of the BsonValue type. This Value can be accessed directly in the format of book ["name"]. For more information about BsonValue, see compile.
4. It is very easy to create a Collection. The sample code is as follows. Note that if a Collection with the specified name already exists, an exception will be thrown. // create Collection using database db = server. getDatabase ("db1"); var res = db. createCollection ("col2 ");
5. insert rows in two cases. The insertion method is basically the same. The Code is as follows: // insert into database db = server. getDatabase ("db1"); Response collection colaa = db. getCollection ("col1"); // use BsonDocument format to insert BsonDocument doc = new BsonDocument {"name", "sse2" },{ "part", "44224 "}}; colaa. insert (doc); // Insert row r1 = new row {name = "sse3", part = "554"}; colaa. insert <row> (r1 );
Note that if a variable in the Custom class is not assigned a value, a Null value will also be written when it is inserted into the database, and this element will not appear in BsonDocument, for example, the running result of the preceding two statements: {"_ id": ObjectId ("5355d8dfccee160de4dca545"), "name": "sse2", "part ": "44224" }{ "_ id": ObjectId ("5355d93bccee16088491c420"), "name": "sse3", "part": "554", "age": null}
Some other methods, such as batch writing, can be referred
6. When updating rows, you must use the constructor to construct Query conditions and Update statements. Query is the Query condition constructor and Update is the Update statement constructor. The code example is as follows: // update your database db = server. getDatabase ("db1"); Response collection colaa = db. getCollection ("col1"); var query = Query. and (Query. EQ ("name", "sse3"), Query. EQ ("part", "554"); var update = MongoDB. driver. builders. update. set ("age", "36"); colaa. update (query, update );
Other updated functions, such as the Save function that integrates update and insert, can be referred
7. When deleting a row, you also need to use the Query constructor to construct a Query condition. The statement will delete the row that meets the condition. The sample code is as follows: // remove rows database db = server. getDatabase ("db1"); Response collection colaa = db. getCollection ("col1"); var query = Query. and (Query. EQ ("name", "sse3"), Query. (EQ ("part", "554"); var res = colaa. remove (query );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.