search
HomeDatabaseMysql TutorialMongoDB结合Flexgrid的简单数据呈现

MongoDB结合Flexgrid的简单数据呈现 本示例以常用的:用户,帖子,评论为基础模型,实现了一个简单的MongoDB结合Flexgrid数据呈现的Demo。由于时间所限,功能上仅提供对MongoDB数据的常用查询操作(分页,排序,查询等)。高手请对我这种菜鸟多些包容。 一,

MongoDB结合Flexgrid的简单数据呈现

 

本示例以常用的:用户,帖子,评论为基础模型,实现了一个简单的MongoDB结合Flexgrid数据呈现的Demo。由于时间所限,功能上仅提供对MongoDB数据的常用查询操作(分页,排序,查询等)。高手请对我这种菜鸟多些包容。

 

一,准备工作:

MongoDB官方下载:

当前最新版本是2.2.0版本。话说,MongoDB的版本更新是相当的快。

 

本示例使用的MongoDB C#版驱动下载:
https://github.com/samus/mongodb-csharp
该驱动附源码和示例程序,有兴趣的朋友可以研究一下,对自己的编码能力会有很大的提高。用VS打开Source文件夹,编译后得到程序集,在自己的项目中引用即可。

 

这是官方邮件提供的 C#版驱动:
https://github.com/mongodb/mongo-csharp-driver/downloads
版本很多,而且基本上都是十多兆的东西,然后,没有然后。

 

如果你不习惯以命令行的方式对MongoDB进行操作和管理。推荐以下客户端管理工具:
1,MongoVUE。这应该是目前应用最广的了。
下载地址:

2,虚拟主机,博客园高手自己开发的MongoDB管理工具:

试用了一下,也是相当不错的!

 

虽然上述管理工具能够以图形化的方式与MongoDB交互,但强烈建议你运行mongo.exe客户端,以命令行的方式对MongoDB进行操作和管理,这会让你对MongoDB有更加直观而深刻的体会。另外,Windows8依然内置了DOS。

 

这里有MongoDB的常用命令:

 

这是Flexgrid的官方网站:

页面顶部有一个硕大的红色Download按钮。

 

TestDriven.net,必不可少的开发和测试工具。本示例需要用它向MongoDB中初始化测试数据。下载地址:

 

二,项目结构:

麻雀虽小,五脏俱全。整个项目结构是一个最原始的三层。直接上图:

三,技术细节:

1,MongoDB数据库操作。
ADO.NET虽然很强大,但我们通常需要自己动手写一个SqlHelper。MongoDB也一样,我们仍有必要在驱动的基础上对常用的数据操作进行封装。下面贴出本鸟写的MongoDB数据库操作类。大多数方法都与数据查询相关。贴出主要代码:

using System; using System.Collections.Generic; using System.Linq; using System.Configuration; using MongoDB; using MongoDB.Linq; using Mcmurphy.Commons.Enumerations; namespace Mcmurphy.DAL { public class MongoDBHelper { #region 基本信息 private static readonly string ConnectionString; private static readonly string DatabaseName; ///

/// 当前Mongo引用 /// private static Mongo mongo; /// /// 初始化数据库配置 /// static MongoDBHelper() { ConnectionString = ConfigurationManager.AppSettings["connString"]; DatabaseName = ConfigurationManager.AppSettings["currentDB"]; } /// /// 当前Mongo对象 /// private static Mongo CurrentMongo { get { return new Mongo(ConnectionString); } } /// /// 当前操作集合 /// 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; } } /// /// 获取当前Mongo数据库对象 /// /// 当前Mongo数据库对象 private static IMongoDatabase GetCurrentDataBase() { return mongo.GetDatabase(DatabaseName); } #endregion #region 数据查询 /// /// 获取排序规则 /// private static IndexOrder GetIndexOrder(DataOrder order) { IndexOrder @orderby = order == DataOrder.Ascending ? IndexOrder.Ascending : IndexOrder.Descending; return orderby; } /// /// 根据条件进行查询,并进行分页 /// /// 类型参数 /// 条件Lamda表达式 /// 当前页索引 /// 分页大小 /// 结果集 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(); } } /// /// 根据条件进行查询,并进行分页和排序 /// /// 类型参数 /// 条件Lamda表达式 /// 当前页索引 /// 分页大小 /// 排序规则 /// 排序字段 /// 结果集 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(); } } /// /// 根据条件查询一个对象 /// /// 类型参数 /// 查询条件Lamda表达式 /// 查询对象 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(); } } /// /// 根据查询条件获取总记录数 /// /// 类型参数 /// 查询条件 /// 记录数 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(); } } /// /// 获取总记录数 /// /// 类型参数 /// 记录数 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 数据插入 /// /// 数据插入 /// /// 类型参数 /// 要插入的数据对象 public 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 数据更新 /// /// 根据查询条件更新数据 /// /// 类型参数 /// 待更新的对象 /// 更新的条件Lamda表达式 public 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(); } } /// /// 更新/插入数据(id是否存在) /// /// 类型参数 /// 待更新的对象 public static void Update(T t) where T : class { try { var currentCollection = GetCollection(); //inserts of updates depends on whether id exists currentCollection.Save(t); } catch (Exception ex) { throw ex; } finally { mongo.Disconnect(); mongo.Dispose(); } } #endregion #region 数据删除 /// /// 数据删除 /// /// 类型参数 /// 查询的条件Lamda表达式 public 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 } }

初次调用的时候,会读取在站点项目下Web.Config文件中配置的数据库服务器地址以及数据库名称。

 

2,Flexgrid加载的数据格式。
在官网上徘徊了很久也没有找到Flexgrid要求的数据格式说明。还好有Firebug,但遗憾的是官方示例采用的是XML格式。如下:

  1  234           ...      ...

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
mongodb php 扩展没有怎么办mongodb php 扩展没有怎么办Nov 06, 2022 am 09:10 AM

mongodb php扩展没有的解决办法:1、在linux中执行“$ sudo pecl install mongo”命令来安装MongoDB的PHP扩展驱动;2、在window中,下载php mongodb驱动二进制包,然后在“php.ini”文件中配置“extension=php_mongo.dll”即可。

Go语言中使用MongoDB:完整指南Go语言中使用MongoDB:完整指南Jun 17, 2023 pm 06:14 PM

MongoDB是一种高性能、开源、文档型的NoSQL数据库,被广泛应用于Web应用、大数据以及云计算领域。而Go语言则是一种快速、开发效率高、代码可维护性强的编程语言。本文将为您完整介绍如何在Go语言中使用MongoDB。一、安装MongoDB在使用MongoDB之前,需要先在您的系统中安装MongoDB。在Linux系统下,可以通过如下命令安装:sudo

Redis和MongoDB的区别与使用场景Redis和MongoDB的区别与使用场景May 11, 2023 am 08:22 AM

Redis和MongoDB都是流行的开源NoSQL数据库,但它们的设计理念和使用场景有所不同。本文将重点介绍Redis和MongoDB的区别和使用场景。Redis和MongoDB简介Redis是一个高性能的数据存储系统,常被用作缓存和消息中间件。Redis以内存为主要存储介质,但它也支持将数据持久化到磁盘上。Redis是一款键值数据库,它支持多种数据结构(例

php7.0怎么安装mongo扩展php7.0怎么安装mongo扩展Nov 21, 2022 am 10:25 AM

php7.0安装mongo扩展的方法:1、创建mongodb用户组和用户;2、下载mongodb源码包,并将源码包放到“/usr/local/src/”目录下;3、进入“src/”目录;4、解压源码包;5、创建mongodb文件目录;6、将文件复制到“mongodb/”目录;7、创建mongodb配置文件并修改配置即可。

php怎么使用mongodb进行增删查改操作php怎么使用mongodb进行增删查改操作Mar 28, 2023 pm 03:00 PM

MongoDB作为一款流行的NoSQL数据库,已经被广泛应用于各种大型Web应用和企业级应用中。而PHP语言也作为一种流行的Web编程语言,与MongoDB的结合也变得越来越重要。在本文中,我们将会学习如何使用PHP语言操作MongoDB数据库进行增删查改的操作。

SpringBoot中logback日志怎么保存到mongoDBSpringBoot中logback日志怎么保存到mongoDBMay 18, 2023 pm 07:01 PM

自定义Appender非常简单,继承一下AppenderBase类即可。可以看到有个AppenderBase,有个UnsynchronizedAppenderBase,还有个AsyncAppenderBase继承了UnsynchronizedAppenderBase。从名字就能看出来区别,异步的、普通的、不加锁的。我们定义一个MongoDBAppender继承UnsynchronizedAppenderBasepublicclassMongoDBAppenderextendsUnsynchron

SpringBoot怎么整合Mongodb实现增删查改SpringBoot怎么整合Mongodb实现增删查改May 13, 2023 pm 02:07 PM

一、什么是MongoDBMongoDB与我们之前熟知的关系型数据库(MySQL、Oracle)不同,MongoDB是一个文档数据库,它具有所需的可伸缩性和灵活性,以及所需的查询和索引。MongoDB将数据存储在灵活的、类似JSON的文档中,这意味着文档的字段可能因文档而异,数据结构也会随着时间的推移而改变。文档模型映射到应用程序代码中的对象,使数据易于处理。MongoDB是一个以分布式数据库为核心的数据库,因此高可用性、横向扩展和地理分布是内置的,并且易于使用。况且,MongoDB是免费的,开源

Swoole与MongoDB的整合:构建高性能的文档数据库系统Swoole与MongoDB的整合:构建高性能的文档数据库系统Jun 14, 2023 am 11:51 AM

在现代企业应用程序开发中,需要处理海量数据和高并发的访问请求。为了满足这些需求,开发人员需要使用高性能的数据库系统,以确保系统的稳定性和可扩展性。本文将介绍如何使用Swoole和MongoDB构建高性能的文档数据库系统。Swoole是一个基于PHP语言开发的异步网络通信框架,它能够大大提高PHP应用程序的性能和并发能力。MongoDB是一种流行的文档数据库,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools