MongoDB simple practice: Only crud

Source: Internet
Author: User
ArticleDirectory
    • 1. add, delete, and modify
    • 2. Paging Query

I wrote a simple example on MongoDB intermittently, involving the basic crud. During the practice, I found many problems, especially the C # Driver Class Library of the new and old versions, which has changed a lot. Therefore, I still cannot figure out some obvious problems and keep a record of them. I look forward to your advice.

I. Development Environment Construction

There is no need to talk nonsense about this. You can search for it. There are many articles on how to build a development environment. If you want to try it yourself, refer to this article in the official document and follow the instructions to configure a local MongoDB ApplicationProgramWindows environment. Of course, if your environment is not windows, you can select the document here and compare the settings.

Ii. Entity description

In the demo, you can see a simple object class Book, which contains four attributes:

Using system; namespace simplemongodbapp {using MongoDB. bson; [serializable] public class book {public book () {} public book (string author, String title, datetime createdate) {This. author = author; this. title = title; this. createdate = createdate;} Public String author {Get; set;} Public String title {Get; set;} public datetime createdate {Get; set ;} /// <summary> /// primary key /// </Summary> Public objectid ID {Get; Set ;}}}

Note that the type of the primary key ID attribute isObjectid. This ID plays an important role in addition, deletion, modification, and query.

3. Simple crud

To be honest, there is nothing to say. It's just a simple class library call, but there are some small accidents in practice, and it feels different from what is described in the document. The example here is mainly based on this article.

1. add, delete, and modify

These three methods are relatively easy to add, delete, and modify. For example, for a simple update method:

 Public void Update (Book query, book DEST) {string connectionstring = config. getcurrentmongodbconnstring (); Login Server = Login server. create (connectionstring); Relational Database database = server. getdatabase (con Fig. currentdbname); Using (server. requeststart (database) {your collection 
  
    books = database. getcollection 
   
     (config. currenttbname); querydocument condition = new querydocument ("_ id", query. ID); // update book = books by ID. findone (condition); If (book! = NULL) {DeST. ID = query. ID; books. Save 
    
      (DEST) ;}}
    
   
  

Here I use the Save function. We can also call the update method described in the document. The final result is the same.

2. Paging Query

Like relational databases, queries can be divided into multiple types, such as exact match, range query, fuzzy match, and group query. The example in this article only performs a simple exact match by field, which is the simplest query. Fortunately, all the current class libraries have corresponding query methods. You can try different types of complex queries by yourself. For simple queries, you can directly call the relevant find method. for paging queries, we usually use setskip and setlimit together to return the query results:

/// <Summary> /// query by PAGE /// </Summary> Public ilist <book> select (INT currentpage, int recordsperpage, book query) {list <book> listresult = new list <book> (); string connectionstring = config. getcurrentmongodbconnstring (); Login Server = Login server. create (connectionstring); Relational Database database = server. getdatabase (config. currentdbname); your collection <book> books = database. getcollection <book> (config. currenttbname); querycomplete condition = query. and (query. eq ("title", query. title); // query result // querycomplete condition = query. and (query. eq ("author", query. author); // No query result // querycomplete condition = query. and (query. eq ("title", query. title), query. eq ("author", query. author); // No query result named cursor <book> cursors = books. find (condition ). setskip (currentpage-1) * recordsperpage ). setlimit (recordsperpage); // books. find (condition); // specify cursor <book> cursors = books. findas <book> (condition ). setskip (currentpage-1) * recordsperpage ). setlimit (recordsperpage); // books. find (condition); // specify cursor <book> cursors = books. findall (). setskip (currentpage-1) * recordsperpage ). setlimit (recordsperpage); // books. find (condition); listresult. addrange (cursors); Return listresult ;}

In practice, I found a problem, that is, when I try to query by author attribute, there is no record (there should be more than one actual matching result, in addition, there are more matching results than query by title), but the results can be queried Based on the title data. It takes a long time to compare the results with the document, and no reason is found .... What is the legendary RP problem? Depressed.

Iv. easymongo of Zhao

For other mongdb related methods, refer to the official documentation. The class libraries are already very rich. Here you may have discovered a problem, that is, when I call the class library method related to MongoDBCodeMore, such as the following section:

 
String connectionstring = config. getcurrentmongodbconnstring (); Login Server = Login server. create (connectionstring); Relational Database database = server. getdatabase (config. currentdbname); Using (server. requeststart (database) {// some code here}

It appears many times, so what, of course, we want to refactor, to use Orm, to call more elegantly ...... And so on. Of course, I have discovered problems in the program. I wanted to encapsulate a layer by myself to make the call more concise and comfortable, but since I saw Zhao's easymongo (it turns out that such a powerful and practical stuff has helped us well. After reading the source code, I feel like his fastreflection is almost complicated, I admit that I can't write it out). I suggest using it directly.

Finally, we will introduce a very good nosql learning resource in China:

Http://blog.nosqlfan.com/

Download Demo: mongodbapp

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.