Simple data presentation using MongoDB with Flexgrid

Source: Internet
Author: User
Tags mongodb version
MongoDB uses Flexgrid's simple data to present this example. This example uses the commonly used: User, post, comment as the basic model to implement a simple Demo of MongoDB's combination of Flexgrid data presentation. Due to time limitations, the function only provides common query operations (paging, sorting, and query) for MongoDB data ). Master, please be more tolerant to cainiao like me. I,

MongoDB uses Flexgrid's simple data to present this example. This example uses the commonly used: User, post, comment as the basic model to implement a simple Demo of MongoDB's combination of Flexgrid data presentation. Due to time limitations, the function only provides common query operations (paging, sorting, and query) for MongoDB data ). Master, please be more tolerant to cainiao like me. I,

Simple data presentation using MongoDB with Flexgrid

In this example, a simple MongoDB Demo is implemented based on the common models of users, posts, and comments. Due to time limitations, the function only provides common query operations (paging, sorting, and query) for MongoDB data ). Master, please be more tolerant to cainiao like me.

I. preparations:

Official download of MongoDB:

The latest version is 2.2.0. In other words, MongoDB version updates are quite fast.

Download the MongoDB C # driver used in this example:
Https://github.com/samus/mongodb-csharp
The driver is accompanied by source code and sample programs. If you are interested, you can study the driver to greatly improve your coding capability. Use VS to open the Source folder, compile and obtain the assembly, and reference it in your project.

This is the C # drive provided by the official mail:
Https://github.com/mongodb/mongo-csharp-driver/downloads
There are many versions, and basically there are more than 10 MB of stuff, and then there is no more.

If you are not used to operating and managing MongoDB using command lines. The following client management tools are recommended:
1. Define vue. This should be the most widely used at present.
:

2. MongoDB management tools developed by bloggers on virtual hosts:

I tried it for a while and it was quite good!

Although the management tool can use the graphic format and mongodbmanager, it strongly recommends that you run the mongo.exe client to operate and manage MongoDB through command lines, which gives you a more intuitive and profound understanding of MongoDB. In addition, Windows 8 still has built-in DOS.

Here are common commands for MongoDB:

This is the official Flexgrid Website:

There is a huge red Download button at the top of the page.

TestDriven.net, an essential development and testing tool. In this example, you need to use it to initialize test data to MongoDB. :

2. project structure:

The sparrow is small and dirty. The entire project structure is the original three layers. Direct:

Iii. Technical Details:

1. MongoDB database operations.
Although ADO. NET is powerful, we usually need to write a SqlHelper by ourselves. The same is true for MongoDB. We still need to encapsulate common data operations based on the driver. The following is a MongoDB database operation class written by this bird. Most methods are related to data query. Paste the main code:

Using System; using System. collections. generic; using System. linq; using System. configuration; using MongoDB. linq; using Mcmurphy. commons. enumerations; namespace Mcmurphy. DAL {public class MongoDBHelper {# region Basic Information private static readonly string ConnectionString; private static readonly string DatabaseName ;///

/// Current Mongo reference ///Private static Mongo mongo ;/// /// Initialize the Database Configuration ///Static MongoDBHelper () {ConnectionString = ConfigurationManager. etettings ["connString"]; DatabaseName = ConfigurationManager. etettings ["currentDB"];} // /// Current Mongo object ///Private static Mongo CurrentMongo {get {return new Mongo (ConnectionString );}}/// /// Current operation set ///Private static IMongoCollection GetCollection () Where T: class {try {mongo = CurrentMongo; mongo. Connect (); IMongoDatabase db = GetCurrentDataBase (); IMongoCollection Collection = db. GetCollection (); Return collection;} catch (Exception ex) {throw ex ;}}/// /// Obtain the current Mongo database object ////// Current Mongo database object Private static IMongoDatabase GetCurrentDataBase () {return mongo. GetDatabase (DatabaseName) ;}# endregion # region Data Query /// /// Obtain the sorting rule ///Private static IndexOrder GetIndexOrder (DataOrder order) {IndexOrder @ orderby = order = DataOrder. Ascending? IndexOrder. Ascending: IndexOrder. Descending; return orderby ;}/// /// Query based on conditions and perform paging ////// Type parameter /// Conditional Lamda expression/// Current page index/// Page size/// Result set Public static IEnumerable GetBySearch (System. Linq. Expressions. Expression > Selector, int pageIndex, int pageSize) where T: class {try {var currentCollection = GetCollection (); Return currentCollection. Find (Selector ). skip (pageIndex-1) * pageSize ). limit (pageSize ). documents. toList ();} catch (Exception ex) {throw ex;} finally {mongo. disconnect (); mongo. dispose ();}}/// /// Query based on conditions and sort by PAGE ////// Type parameter /// Conditional Lamda expression/// Current page index/// Page size/// Sorting rules/// Sorting Field/// Result set Public static IEnumerable GetBySearch (System. Linq. Expressions. Expression > Selector, int pageIndex, int pageSize, DataOrder order, string orderField) where T: class {try {IndexOrder orderby = GetIndexOrder (order); var currentCollection = GetCollection (); Return currentCollection. Find (Selector ). sort (orderField, orderby ). skip (pageIndex-1) * pageSize ). limit (pageSize ). documents. toList ();} catch (Exception ex) {throw ex;} finally {mongo. disconnect (); mongo. dispose ();}}/// /// Query an object based on conditions ////// Type parameter /// Query condition Lamda expression/// Query object Public static T GetOneBySearch (System. Linq. Expressions. Expression > Selector) where T: class {try {var currentCollection = GetCollection (); Return currentCollection. FindOne (Selector);} catch (Exception ex) {throw ex;} finally {mongo. Disconnect (); mongo. Dispose ();}}/// /// Obtain the total number of records based on query conditions ////// Type parameter /// Query Conditions/// Number of records Public static long GetTotalCount (System. Linq. Expressions. Expression > Selector) where T: class {try {var currentCollection = GetCollection (); Return currentCollection. count (selector);} catch (Exception ex) {throw ex;} finally {mongo. disconnect (); mongo. dispose ();}}/// /// Obtain the total number of records ////// Type parameter /// Number of records Public static long GetTotalCount () Where T: class {try {var currentCollection = GetCollection (); Return currentCollection. count ();} catch (Exception ex) {throw ex;} finally {mongo. disconnect (); mongo. dispose () ;}# endregion # region data insertion /// /// Insert data ////// Type parameter /// Data Object TO BE INSERTEDPublic static void Insert (T t) where T: class {try {var currentCollection = GetCollection (); CurrentCollection. insert (t);} catch (Exception ex) {throw ex;} finally {mongo. disconnect (); mongo. dispose () ;}# endregion # region data update /// /// Update data based on query conditions ////// Type parameter /// Objects to be updated/// Updated conditional Lamda expressionPublic static void Update (T t, System. Linq. Expressions. Expression > Selector) where T: class {try {var currentCollection = GetCollection (); CurrentCollection. Update (T, selector);} catch (Exception ex) {throw ex;} finally {mongo. Disconnect (); mongo. Dispose ();}}/// /// Update/insert data (whether the id exists )////// Type parameter /// Objects to be updatedPublic static void Update (T t) where T: class {try {var currentCollection = GetCollection (); // Inserts of updates depends on whether id existscurrentCollection. save (t);} catch (Exception ex) {throw ex;} finally {mongo. disconnect (); mongo. dispose () ;}# endregion # Delete region data /// /// Delete data ////// Type parameter /// Conditional Lambda expression for queryPublic static void Delete (System. Linq. Expressions. Expression > Selector) where T: class {try {var currentCollection = GetCollection (); CurrentCollection. Remove (Selector);} catch (Exception ex) {throw ex;} finally {mongo. Disconnect (); mongo. Dispose () ;}# endregion }}

During the initial call, the database server address and database name configured in the Web. Config file under the site project will be read.

2. Data Format loaded by Flexgrid.
After a long time on the official website, I did not find the data format description required by Flexgrid. Fortunately, there is Firebug, but unfortunately the official example uses XML format. As follows:

1 234 ......

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.