基本信息
源码名称:C# mongodb 查询工具源码(wpf/mvvm)下载
源码大小:0.72M
文件格式:.zip
开发语言:C#
更新时间:2014-12-24
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using MongoDB.Driver;

namespace MongoDBManagementStudio.Model
{
    public class MongoDbCSharpQuery : IMongoQuery
    {
        private ICursor _cursor = null;
        private Mongo _db = null;

        public IEnumerable RunQuery(string server, string database, string port, string query)
        {
            if (database == string.Empty)
                throw new QueryValidationException("You must specify a non-empty database name");

            if (query == string.Empty)
                throw new QueryValidationException("You must specify a non-empty query");

            _db = new Mongo(string.Format("Server={0}:{1}", server, port));
            IList<DictionaryBase> documents = new List<DictionaryBase>();

            try
            {
                _db.Connect();
            }
            catch (SocketException ex)
            {
                throw new UnknownServerException(string.Format("Unknown server: {0}:{1}", server, port), ex);
            }

            string[] queryParts = query.Split(':');
            string collection = queryParts[0];

            if (queryParts.Length > 1)
            {
                Document spec = new Document();
                string where = queryParts[1];
                const string LIMIT_TEXT = " limit ";
                int limitIndex = where.IndexOf(LIMIT_TEXT);
                int limit = 0;

                if (limitIndex > -1)
                {
                    string limitText;

                    if (int.TryParse(where.Substring(limitIndex   LIMIT_TEXT.Length), out limit))
                        where = where.Substring(0, limitIndex);
                }

                spec.Append("$where", new Code(where));
                _cursor = _db[database][collection].Find(spec, limit, 0);
            }
            else
                _cursor = _db[database][collection].FindAll();

            return _cursor.Documents;
            //Document d = db[database].SendCommand("db.test.find();");
        }

        public void Dispose()
        {
            if (_cursor != null)
                _cursor.Dispose();

            if (_db != null)
                _db.Disconnect();
        }

        public IList<string> GetCollections(string server, string database, string port)
        {
            _db = new Mongo(string.Format("Server={0}:{1}", server, port));

            try
            {
                _db.Connect();
            }
            catch (SocketException ex)
            {
                throw new UnknownServerException(string.Format("Unknown server: {0}:{1}", server, port), ex);
            }

            IList<String> collectionNames = _db[database].GetCollectionNames();
            var filteredCollections = new List<string>();
            var hiddenCollectionCriteria = new string[] {"cubicle", "tmp.", ".$", "system.indexes"};

            foreach (string collectionName in collectionNames)
            {
                if (!hiddenCollectionCriteria.Any(criteria => collectionName.Contains(criteria)))
                {
                    int periodIndex = collectionName.IndexOf(".");
                    string collection = collectionName;

                    if (periodIndex >= 0 && periodIndex != (collectionName.Length - 1))
                        collection = collectionName.Substring(collectionName.IndexOf(".")   1);

                    filteredCollections.Add(collection);
                }
            }

            return filteredCollections;
        }
    }
}