/// <summary>
        ///     当前对象的MONGO命令
        /// </summary>
        /// <param name="mMongoCommand">命令对象</param>
        /// <param name="ShowMsgBox"></param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoCommand(MongoCommand mMongoCommand, Boolean ShowMsgBox = true)
        {
            var ResultCommandList = new List <CommandResult>();

            var mCommandResult = new CommandResult(new BsonDocument());

            try
            {
                switch (mMongoCommand.RunLevel)
                {
                case MongoDbHelper.PathLv.CollectionLv:
                    if (String.IsNullOrEmpty(mMongoCommand.CommandString))
                    {
                        mCommandResult = ExecuteMongoColCommand(mMongoCommand.cmdDocument,
                                                                SystemManager.GetCurrentCollection());
                    }
                    else
                    {
                        mCommandResult = ExecuteMongoColCommand(mMongoCommand.CommandString,
                                                                SystemManager.GetCurrentCollection());
                    }
                    break;

                case MongoDbHelper.PathLv.DatabaseLv:
                    mCommandResult = ExecuteMongoDBCommand(mMongoCommand.cmdDocument,
                                                           SystemManager.GetCurrentDataBase());
                    break;

                case MongoDbHelper.PathLv.InstanceLv:
                    mCommandResult = ExecuteMongoSvrCommand(mMongoCommand.cmdDocument,
                                                            SystemManager.GetCurrentServer());
                    break;

                default:
                    break;
                }
                ResultCommandList.Add(mCommandResult);
                if (ShowMsgBox)
                {
                    MyMessageBox.ShowMessage(mMongoCommand.CommandString, mMongoCommand.CommandString + " Result",
                                             MongoDbHelper.ConvertCommandResultlstToString(ResultCommandList), true);
                }
            }
            catch (IOException ex)
            {
                SystemManager.ExceptionDeal(ex, mMongoCommand.CommandString,
                                            "IOException,Try to set Socket TimeOut more long at connection config");
            }
            catch (Exception ex)
            {
                SystemManager.ExceptionDeal(ex, mMongoCommand.CommandString);
            }

            return(mCommandResult);
        }
示例#2
0
        public static void Test_LoadMongoDetail_01()
        {
            Trace.WriteLine("Test_LoadMongoDetail_01");
            Trace.WriteLine();

            RapideDdl.InitMongoClassMap();

            if (BsonClassMap.IsClassMapRegistered(typeof(RapideDdl_Base)))
            {
                BsonClassMap map = BsonClassMap.LookupClassMap(typeof(RapideDdl_Base));
                Trace.WriteLine("change existing class map");
                BsonMemberMap memberMap = map.GetMemberMap("infos");
                memberMap.SetSerializationOptions(DictionarySerializationOptions.ArrayOfDocuments);
            }
            else
            {
                Trace.WriteLine("register class map");
                BsonClassMap.RegisterClassMap <RapideDdl_Base>(cm =>
                {
                    cm.AutoMap();
                    cm.GetMemberMap(c => c.infos).SetSerializationOptions(DictionarySerializationOptions.ArrayOfDocuments);
                });
            }
            Trace.WriteLine();

            string query = "{ _id: 35105 }";
            MongoCursor <BsonDocument> cursor = MongoCommand.GetDatabase(null, "dl").GetCollection("RapideDdl_Detail").zFind <BsonDocument>(new QueryDocument(BsonSerializer.Deserialize <BsonDocument>(query)));
            int i = 1;

            foreach (BsonDocument document in cursor)
            {
                BsonDocument         document2  = (BsonDocument)document["download"];
                RapideDdl_PostDetail postDetail = BsonSerializer.Deserialize <RapideDdl_PostDetail>(document2);
                //MongoDB.Bson.Serialization.IBsonSerializationOptions options;
                //MongoDB.Bson.Serialization.BsonSerializer.Serialize()
                //Serialization.Options
                //SerializationOptions
                //MongoDB.Bson.Serialization.Options.RepresentationSerializationOptions
                //document2 = postDetail.ToBsonDocument(new DictionarySerializationOptions(DictionaryRepresentation.ArrayOfDocuments));
                //DictionarySerializationOptions.
                //BsonClassMap.RegisterClassMap<RapideDdl_PostDetail>(cm => { cm.MapProperty(c => c.SomeProperty); cm.MapProperty(c => c.AnotherProperty); });
                //document2 = postDetail.ToBsonDocument();
                Trace.WriteLine("document no {0}", i++);
                //DocumentSerializationOptions options = new DocumentSerializationOptions();
                //Trace.WriteLine(postDetail.ToJson(new DictionarySerializationOptions(DictionaryRepresentation.ArrayOfDocuments)));

                //RapideDdl_PostDetail  RapideDdl_Base
                //BsonClassMap<RapideDdl_Base> map = BsonClassMap.LookupClassMap(typeof(RapideDdl_Base));


                //Trace.WriteLine(document.zToJson());
                Trace.WriteLine(postDetail.zToJson());
                Trace.WriteLine();
            }
        }
示例#3
0
 /// <summary>
 ///     执行聚合
 /// </summary>
 /// <param name="aggregateDoc"></param>
 /// <returns></returns>
 /// <param name="collectionName"></param>
 public static CommandResult Aggregate(BsonArray aggregateDoc, string collectionName)
 {
     //db.runCommand( { aggregate: "people", pipeline: [<pipeline>] } )
     var aggrCmd = new CommandDocument
         {
             new BsonElement("aggregate", new BsonString(collectionName)),
             new BsonElement("pipeline", aggregateDoc)
         };
     var aggregateCommand = new MongoCommand(aggrCmd, EnumMgr.PathLevel.Database);
     return ExecuteMongoCommand(aggregateCommand);
 }
示例#4
0
 /// <summary>
 ///     执行Count(指定路劲)
 /// </summary>
 /// <param name="QueryDoc"></param>
 /// <param name="databaseName"></param>
 /// <param name="collectionName"></param>
 /// <returns></returns>
 public static CommandResult Count(BsonDocument QueryDoc, string databaseName, string collectionName)
 {
     //db.runCommand( { aggregate: "people", pipeline: [<pipeline>] } )
     var aggrCmd = new CommandDocument
         {
             new BsonElement("count", new BsonString(collectionName)),
             new BsonElement("query", QueryDoc)
         };
     var aggregateCommand = new MongoCommand(aggrCmd, EnumMgr.PathLevel.Database, databaseName);
     return ExecuteMongoCommand(aggregateCommand);
 }
        /// <summary>
        ///     在指定数据库执行指定命令
        /// </summary>
        /// <param name="mMongoCommand"></param>
        /// <param name="mongoDB"></param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoDBCommand(MongoCommand mMongoCommand, MongoDatabase mongoDB)
        {
            var Command = new CommandDocument {
                { mMongoCommand.CommandString, 1 }
            };

            if (mMongoCommand.RunLevel == MongoDbHelper.PathLv.DatabaseLv)
            {
                return(ExecuteMongoDBCommand(Command, mongoDB));
            }
            throw new Exception();
        }
示例#6
0
        /// <summary>
        ///     执行聚合
        /// </summary>
        /// <param name="aggregateDoc"></param>
        /// <returns></returns>
        /// <param name="collectionName"></param>
        public static CommandResult Aggregate(BsonArray aggregateDoc, string collectionName)
        {
            //db.runCommand( { aggregate: "people", pipeline: [<pipeline>] } )
            var aggrCmd = new CommandDocument
            {
                new BsonElement("aggregate", new BsonString(collectionName)),
                new BsonElement("pipeline", aggregateDoc)
            };
            var aggregateCommand = new MongoCommand(aggrCmd, EnumMgr.PathLevel.Database);

            return(ExecuteMongoCommand(aggregateCommand));
        }
示例#7
0
        /// <summary>
        ///     在指定服务器上执行指定命令
        /// </summary>
        /// <param name="mMongoCommand"></param>
        /// <param name="mongosrv"></param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoSvrCommand(MongoCommand mMongoCommand, MongoServer mongosrv)
        {
            var command = new CommandDocument {
                { mMongoCommand.CommandString, 1 }
            };

            if (mMongoCommand.RunLevel == EnumMgr.PathLevel.Database)
            {
                throw new Exception();
            }
            return(ExecuteMongoSvrCommand(command, mongosrv));
        }
示例#8
0
        /// <summary>
        ///     执行Count(指定路劲)
        /// </summary>
        /// <param name="QueryDoc"></param>
        /// <param name="databaseName"></param>
        /// <param name="collectionName"></param>
        /// <returns></returns>
        public static CommandResult Count(BsonDocument QueryDoc, string databaseName, string collectionName)
        {
            //db.runCommand( { aggregate: "people", pipeline: [<pipeline>] } )
            var aggrCmd = new CommandDocument
            {
                new BsonElement("count", new BsonString(collectionName)),
                new BsonElement("query", QueryDoc)
            };
            var aggregateCommand = new MongoCommand(aggrCmd, EnumMgr.PathLevel.Database, databaseName);

            return(ExecuteMongoCommand(aggregateCommand));
        }
示例#9
0
        /// <summary>
        ///     当前对象的MONGO命令
        /// </summary>
        /// <param name="mMongoCommand">命令对象</param>
        /// <param name="Obj"></param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoCommand(MongoCommand mMongoCommand)
        {
            var resultCommandList = new List <CommandResult>();

            var mCommandResult = new CommandResult(new BsonDocument());

            try
            {
                switch (mMongoCommand.RunLevel)
                {
                case EnumMgr.PathLevel.Collection:
                    if (string.IsNullOrEmpty(mMongoCommand.CommandString))
                    {
                        mCommandResult = ExecuteMongoColCommand(mMongoCommand.CmdDocument,
                                                                RuntimeMongoDbContext.GetCurrentCollection());
                    }
                    else
                    {
                        mCommandResult = ExecuteMongoColCommand(mMongoCommand.CommandString,
                                                                RuntimeMongoDbContext.GetCurrentCollection());
                    }
                    break;

                case EnumMgr.PathLevel.Database:
                    mCommandResult = ExecuteMongoDBCommand(mMongoCommand.CmdDocument,
                                                           RuntimeMongoDbContext.GetCurrentDataBase());
                    break;

                case EnumMgr.PathLevel.Instance:
                    mCommandResult = ExecuteMongoSvrCommand(mMongoCommand.CmdDocument,
                                                            RuntimeMongoDbContext.GetCurrentServer());
                    break;

                default:
                    break;
                }
                resultCommandList.Add(mCommandResult);
            }
            catch (IOException ex)
            {
                Utility.ExceptionDeal(ex, mMongoCommand.CommandString,
                                      "IOException,Try to set Socket TimeOut more long at connection config");
            }
            catch (Exception ex)
            {
                Utility.ExceptionDeal(ex, mMongoCommand.CommandString);
            }

            return(mCommandResult);
        }
        /// <summary>
        ///     当前对象的MONGO命令
        /// </summary>
        /// <param name="mMongoCommand">命令对象</param>
        /// <param name="ShowMsgBox"></param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoCommand(MongoCommand mMongoCommand, Boolean ShowMsgBox = true)
        {
            var ResultCommandList = new List<CommandResult>();

            var mCommandResult = new CommandResult(new BsonDocument());
            try
            {
                switch (mMongoCommand.RunLevel)
                {
                    case MongoDbHelper.PathLv.CollectionLv:
                        if (String.IsNullOrEmpty(mMongoCommand.CommandString))
                        {
                            mCommandResult = ExecuteMongoColCommand(mMongoCommand.cmdDocument,
                                SystemManager.GetCurrentCollection());
                        }
                        else
                        {
                            mCommandResult = ExecuteMongoColCommand(mMongoCommand.CommandString,
                                SystemManager.GetCurrentCollection());
                        }
                        break;
                    case MongoDbHelper.PathLv.DatabaseLv:
                        mCommandResult = ExecuteMongoDBCommand(mMongoCommand.cmdDocument,
                            SystemManager.GetCurrentDataBase());
                        break;
                    case MongoDbHelper.PathLv.InstanceLv:
                        mCommandResult = ExecuteMongoSvrCommand(mMongoCommand.cmdDocument,
                            SystemManager.GetCurrentServer());
                        break;
                    default:
                        break;
                }
                ResultCommandList.Add(mCommandResult);
                if (ShowMsgBox)
                    MyMessageBox.ShowMessage(mMongoCommand.CommandString, mMongoCommand.CommandString + " Result",
                        MongoDbHelper.ConvertCommandResultlstToString(ResultCommandList), true);
            }
            catch (IOException ex)
            {
                SystemManager.ExceptionDeal(ex, mMongoCommand.CommandString,
                    "IOException,Try to set Socket TimeOut more long at connection config");
            }
            catch (Exception ex)
            {
                SystemManager.ExceptionDeal(ex, mMongoCommand.CommandString);
            }

            return mCommandResult;
        }
示例#11
0
        /// <summary>
        /// 在指定数据库执行指定命令
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="mongoDB"></param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoDBCommand(MongoCommand cmd, MongoDatabase mongoDB)
        {
            var Command = new CommandDocument {
                { cmd.CommandString, 1 }
            };

            if (cmd.RunLevel == PathLv.DatabaseLV)
            {
                return(ExecuteMongoDBCommand(Command, mongoDB));
            }
            else
            {
                throw new Exception();
            }
        }
        /// <summary>
        /// 在指定服务器上执行指定命令
        /// </summary>
        /// <param name="mMongoCommand"></param>
        /// <param name="mongosrv"></param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoSvrCommand(MongoCommand mMongoCommand, MongoServer mongosrv)
        {
            var Command = new CommandDocument {
                { mMongoCommand.CommandString, 1 }
            };

            if (mMongoCommand.RunLevel == PathLv.DatabaseLV)
            {
                throw new Exception();
            }
            else
            {
                return(ExecuteMongoSvrCommand(Command, mongosrv));
            }
        }
 public static CommandResult Aggregate(BsonArray AggregateDoc)
 {
     //db.runCommand( { aggregate: "people", pipeline: [<pipeline>] } )
     try
     {
         CommandDocument agg = new CommandDocument();
         agg.Add(new BsonElement("aggregate", new BsonString(SystemManager.GetCurrentCollection().Name)));
         agg.Add(new BsonElement("pipeline", AggregateDoc));
         MongoCommand Aggregate_Command = new MongoCommand(agg, PathLv.DatabaseLV);
         return ExecuteMongoCommand(Aggregate_Command, false);
     }
     catch (Exception ex)
     {
         SystemManager.ExceptionDeal(ex);
         return new CommandResult(new BsonDocument());
     }
 }
 /// <summary>
 /// 执行聚合
 /// </summary>
 /// <param name="AggregateDoc"></param>
 /// <returns></returns>
 public static CommandResult Aggregate(BsonArray AggregateDoc)
 {
     //db.runCommand( { aggregate: "people", pipeline: [<pipeline>] } )
     try
     {
         CommandDocument agg = new CommandDocument();
         agg.Add(new BsonElement("aggregate", new BsonString(SystemManager.GetCurrentCollection().Name)));
         agg.Add(new BsonElement("pipeline", AggregateDoc));
         MongoCommand Aggregate_Command = new MongoCommand(agg, PathLv.DatabaseLV);
         return(ExecuteMongoCommand(Aggregate_Command, false));
     }
     catch (Exception ex)
     {
         SystemManager.ExceptionDeal(ex);
         return(new CommandResult(new BsonDocument()));
     }
 }
示例#15
0
        /// <summary>
        ///     当前对象的MONGO命令
        /// </summary>
        /// <param name="mMongoCommand">命令对象</param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoCommand(MongoCommand mMongoCommand)
        {
            var resultCommandList = new List<CommandResult>();

            var mCommandResult = new CommandResult(new BsonDocument());
            try
            {
                switch (mMongoCommand.RunLevel)
                {
                    case EnumMgr.PathLevel.Collection:
                        if (string.IsNullOrEmpty(mMongoCommand.CommandString))
                        {
                            mCommandResult = ExecuteMongoColCommand(mMongoCommand.CmdDocument,
                                RuntimeMongoDbContext.GetCurrentCollection());
                        }
                        else
                        {
                            mCommandResult = ExecuteMongoColCommand(mMongoCommand.CommandString,
                                RuntimeMongoDbContext.GetCurrentCollection());
                        }
                        break;
                    case EnumMgr.PathLevel.Database:
                        mCommandResult = ExecuteMongoDBCommand(mMongoCommand.CmdDocument,
                            RuntimeMongoDbContext.GetCurrentDataBase());
                        break;
                    case EnumMgr.PathLevel.Instance:
                        mCommandResult = ExecuteMongoSvrCommand(mMongoCommand.CmdDocument,
                            RuntimeMongoDbContext.GetCurrentServer());
                        break;
                }
                resultCommandList.Add(mCommandResult);
            }
            catch (IOException ex)
            {
                Utility.ExceptionDeal(ex, mMongoCommand.CommandString,
                    "IOException,Try to set Socket TimeOut more long at connection config");
            }
            catch (Exception ex)
            {
                Utility.ExceptionDeal(ex, mMongoCommand.CommandString);
            }

            return mCommandResult;
        }
示例#16
0
 /// <summary>
 ///     执行聚合
 /// </summary>
 /// <param name="aggregateDoc"></param>
 /// <returns></returns>
 /// <param name="collectionName"></param>
 public static CommandResult Aggregate(BsonArray aggregateDoc, string collectionName)
 {
     //db.runCommand( { aggregate: "people", pipeline: [<pipeline>] } )
     try
     {
         var agg = new CommandDocument
         {
             new BsonElement("aggregate", new BsonString(collectionName)),
             new BsonElement("pipeline", aggregateDoc)
         };
         var aggregateCommand = new MongoCommand(agg, EnumMgr.PathLevel.Database);
         return(ExecuteMongoCommand(aggregateCommand));
     }
     catch (Exception ex)
     {
         Utility.ExceptionDeal(ex);
         return(new CommandResult(new BsonDocument()));
     }
 }
示例#17
0
        public static void Test_RapideDdl_MongoUpdateDetailItemList_01(string query)
        {
            int imagesIndex = 11;
            int nb          = 0;

            MongoCommand.UpdateDocuments("dl", "Test_RapideDdl_Detail", query,
                                         doc =>
            {
                Trace.WriteLine("update document {0}", doc["_id"]);
                doc["download"].AsBsonDocument.Remove("images2");
                BsonValue images          = doc["download"]["images"];
                BsonElement elementImages = doc["download"].AsBsonDocument.GetElement(imagesIndex);
                if (elementImages.Name != "images")
                {
                    Trace.WriteLine("error element index {0} is'nt images but {1}", imagesIndex, elementImages.Name);
                    return;
                }
                if (!images.IsBsonArray)
                {
                    Trace.WriteLine("error element images is'nt an array");
                    return;
                }
                BsonArray images2 = new BsonArray();
                foreach (BsonValue image in images.AsBsonArray)
                {
                    if (image.IsBsonDocument)
                    {
                        images2.Add(image["Source"]);
                    }
                    else
                    {
                        Trace.WriteLine("error value of images is'nt a document");
                    }
                }
                //doc["download"].AsBsonDocument.Add("images2", images2);
                //doc["download"].AsBsonDocument.InsertAt(imagesIndex + 1, new BsonElement("images2", images2));
                doc["download"].AsBsonDocument.Remove("images");
                doc["download"].AsBsonDocument.InsertAt(imagesIndex, new BsonElement("images", images2));
                nb++;
            });
            Trace.WriteLine("{0} documents updated", nb);
        }
示例#18
0
        public static IEnumerable <TestPrint> FindPrintFromMongo(DownloadAutomateManager downloadAutomate, string query, int limit = 0, string sort = null)
        {
            if (sort == null)
            {
                sort = "{ 'download.title': 1 }";
            }
            //var query2 = pb.Data.Mongo.MongoCommand.Find("dl", "RapideDdl_Detail2", query, limit: limit, sort: sort, fields: "{ '_id': 1, 'download.title': 1, 'download.creationDate': 1, 'download.category': 1, 'download.isPrint': 1, 'download.sourceUrl': 1  }");
            var query2 = MongoCommand.Find("dl", "RapideDdl_Detail2", query, limit: limit, sort: sort, fields: "{ '_id': 1, 'download.title': 1, 'download.creationDate': 1, 'download.category': 1, 'download.postType': 1, 'download.sourceUrl': 1  }");
            var query3 = query2.Select(document =>
                                       new TestPrint
            {
                title    = document["download"]["title"].AsString,
                category = document["download"]["category"].AsString,
                //isPrint = document["download"]["isPrint"].AsBoolean
                postType = (PrintType)document["download"]["postType"].AsInt32
            }
                                       );

            return(query3);
        }
示例#19
0
        /// <summary>
        ///     当前对象的MONGO命令
        /// </summary>
        /// <param name="mMongoCommand">命令对象</param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoCommand(MongoCommand mMongoCommand)
        {
            var resultCommandList = new List <CommandResult>();

            var mCommandResult = new CommandResult(new BsonDocument());

            switch (mMongoCommand.RunLevel)
            {
            case EnumMgr.PathLevel.CollectionAndView:
                if (string.IsNullOrEmpty(mMongoCommand.CommandString))
                {
                    mCommandResult = ExecuteMongoColCommand(mMongoCommand.CmdDocument,
                                                            RuntimeMongoDbContext.GetCurrentCollection());
                }
                else
                {
                    mCommandResult = ExecuteMongoColCommand(mMongoCommand.CommandString,
                                                            RuntimeMongoDbContext.GetCurrentCollection());
                }
                break;

            case EnumMgr.PathLevel.Database:
                if (string.IsNullOrEmpty(mMongoCommand.DatabaseName))
                {
                    mCommandResult = ExecuteMongoDBCommand(mMongoCommand.CmdDocument, RuntimeMongoDbContext.GetCurrentDataBase());
                }
                else
                {
                    var db = RuntimeMongoDbContext.GetCurrentClient().GetDatabase(mMongoCommand.DatabaseName);
                    mCommandResult = ExecuteMongoDBCommand(mMongoCommand.CmdDocument, db);
                }
                break;

            case EnumMgr.PathLevel.Instance:
                mCommandResult = ExecuteMongoSvrCommand(mMongoCommand.CmdDocument,
                                                        RuntimeMongoDbContext.GetCurrentServer());
                break;
            }
            resultCommandList.Add(mCommandResult);
            return(mCommandResult);
        }
示例#20
0
        /// <summary>
        /// 当前对象的MONGO命令
        /// </summary>
        /// <param name="cmd">命令对象</param>
        /// <returns></returns>
        public static void ExecuteMongoCommand(MongoCommand cmd)
        {
            var Command = new CommandDocument {
                { cmd.CommandString, 1 }
            };
            List <CommandResult> ResultCommandList = new List <CommandResult>();

            CommandResult rtn = new CommandResult();

            try
            {
                switch (cmd.RunLevel)
                {
                case PathLv.CollectionLV:
                    rtn = ExecuteMongoColCommand(cmd.CommandString, SystemManager.GetCurrentCollection());
                    break;

                case PathLv.DatabaseLV:
                    rtn = ExecuteMongoDBCommand(Command, SystemManager.GetCurrentDataBase());
                    break;

                case PathLv.ServerLV:
                    rtn = ExecuteMongoSvrCommand(Command, SystemManager.GetCurrentServer());
                    break;

                default:
                    break;
                }
                ResultCommandList.Add(rtn);
                MyMessageBox.ShowMessage(cmd.CommandString, cmd.CommandString + " Result", MongoDBHelper.ConvertCommandResultlstToString(ResultCommandList), true);
            }
            catch (System.IO.IOException ex)
            {
                MyMessageBox.ShowMessage(cmd.CommandString, "IOException,Try to set Socket TimeOut more long at connection config", ex.ToString(), true);
            }
            catch (Exception ex)
            {
                SystemManager.ExceptionDeal(ex, cmd.CommandString);
            }
        }
示例#21
0
        public static BsonDocument ReadMongoDocument(string database, string collectionName, object key, string itemName, string server = null)
        {
            MongoDatabase mdb = MongoCommand.GetDatabase(server, database);

            Trace.Write("read mongo document key \"{0}\" from collection \"{1}\" ", key, collectionName);
            Trace.WriteLine("server \"mongodb://{0}\" database \"{1}\"", mdb.Server.Instance.Address, mdb.Name);
            MongoCollection collection = mdb.GetCollection(collectionName);
            BsonDocument    document   = collection.FindOneByIdAs <BsonDocument>(BsonValue.Create(key));

            if (document == null || !document.Contains(itemName))
            {
                Trace.WriteLine("document is null or does'nt contain element \"{0}\"", itemName);
                return(null);
            }
            var element = document[itemName];

            if (element == null || !(element is BsonDocument))
            {
                Trace.WriteLine("element \"{0}\" is null or is'nt a BsonDocument", itemName);
                return(null);
            }
            document = element as BsonDocument;
            return(document);
        }
 /// <summary>
 /// 在指定服务器上执行指定命令
 /// </summary>
 /// <param name="mMongoCommand"></param>
 /// <param name="mongosrv"></param>
 /// <returns></returns>
 public static CommandResult ExecuteMongoSvrCommand(MongoCommand mMongoCommand, MongoServer mongosrv)
 {
     var Command = new CommandDocument { { mMongoCommand.CommandString, 1 } };
     if (mMongoCommand.RunLevel == PathLv.DatabaseLV)
     {
         throw new Exception();
     }
     else
     {
         return ExecuteMongoSvrCommand(Command, mongosrv);
     }
 }
示例#23
0
        /// <summary>
        ///     当前对象的MONGO命令
        /// </summary>
        /// <param name="mMongoCommand">命令对象</param>
        /// <returns></returns>
        public static CommandResult ExecuteMongoCommand(MongoCommand mMongoCommand)
        {
            var resultCommandList = new List<CommandResult>();

            var mCommandResult = new CommandResult(new BsonDocument());
            switch (mMongoCommand.RunLevel)
            {
                case EnumMgr.PathLevel.CollectionAndView:
                    if (string.IsNullOrEmpty(mMongoCommand.CommandString))
                    {
                        mCommandResult = ExecuteMongoColCommand(mMongoCommand.CmdDocument,
                            RuntimeMongoDbContext.GetCurrentCollection());
                    }
                    else
                    {
                        mCommandResult = ExecuteMongoColCommand(mMongoCommand.CommandString,
                            RuntimeMongoDbContext.GetCurrentCollection());
                    }
                    break;
                case EnumMgr.PathLevel.Database:
                    if (string.IsNullOrEmpty(mMongoCommand.DatabaseName))
                    {
                        mCommandResult = ExecuteMongoDBCommand(mMongoCommand.CmdDocument, RuntimeMongoDbContext.GetCurrentDataBase());
                    }
                    else
                    {
                        var db = RuntimeMongoDbContext.GetCurrentClient().GetDatabase(mMongoCommand.DatabaseName);
                        mCommandResult = ExecuteMongoDBCommand(mMongoCommand.CmdDocument, db);
                    }
                    break;
                case EnumMgr.PathLevel.Instance:
                    mCommandResult = ExecuteMongoSvrCommand(mMongoCommand.CmdDocument,
                        RuntimeMongoDbContext.GetCurrentServer());
                    break;
            }
            resultCommandList.Add(mCommandResult);
            return mCommandResult;
        }
 /// <summary>
 ///     在指定数据库执行指定命令
 /// </summary>
 /// <param name="mMongoCommand"></param>
 /// <param name="mongoDB"></param>
 /// <returns></returns>
 public static CommandResult ExecuteMongoDBCommand(MongoCommand mMongoCommand, MongoDatabase mongoDB)
 {
     var Command = new CommandDocument {{mMongoCommand.CommandString, 1}};
     if (mMongoCommand.RunLevel == MongoDbHelper.PathLv.DatabaseLv)
     {
         return ExecuteMongoDBCommand(Command, mongoDB);
     }
     throw new Exception();
 }
示例#25
0
 /// <summary>
 ///     执行聚合
 /// </summary>
 /// <param name="aggregateDoc"></param>
 /// <returns></returns>
 /// <param name="collectionName"></param>
 public static CommandResult Aggregate(BsonArray aggregateDoc, string collectionName)
 {
     //db.runCommand( { aggregate: "people", pipeline: [<pipeline>] } )
     try
     {
         var agg = new CommandDocument
         {
             new BsonElement("aggregate", new BsonString(collectionName)),
             new BsonElement("pipeline", aggregateDoc)
         };
         var aggregateCommand = new MongoCommand(agg, EnumMgr.PathLevel.Database);
         return ExecuteMongoCommand(aggregateCommand);
     }
     catch (Exception ex)
     {
         Utility.ExceptionDeal(ex);
         return new CommandResult(new BsonDocument());
     }
 }
示例#26
0
 /// <summary>
 ///     在指定服务器上执行指定命令
 /// </summary>
 /// <param name="mMongoCommand"></param>
 /// <param name="mongosrv"></param>
 /// <returns></returns>
 public static CommandResult ExecuteMongoSvrCommand(MongoCommand mMongoCommand, MongoServer mongosrv)
 {
     var command = new CommandDocument {{mMongoCommand.CommandString, 1}};
     if (mMongoCommand.RunLevel == EnumMgr.PathLevel.Database)
     {
         throw new Exception();
     }
     return ExecuteMongoSvrCommand(command, mongosrv);
 }
 /// <summary>
 /// 在指定数据库执行指定命令
 /// </summary>
 /// <param name="cmd"></param>
 /// <param name="mongoDB"></param>
 /// <returns></returns>
 public static CommandResult RunMongoCommandAtMongoDB(MongoCommand cmd, MongoDatabase mongoDB)
 {
     var Command = new CommandDocument { { cmd.CommandString, 1 } };
     if (cmd.RunLevel == PathLv.DatabaseLV)
     {
         return ExecuteMongoDBCommand(Command, mongoDB);
     }
     else
     {
         throw new Exception();
     }
 }
示例#28
0
 public static void Test_ViewImagesCache_01()
 {
     //RunSource.CurrentRunSource.View(MongoCommand.Find<MongoImage>("dl", "Images", "{}", sort: "{ 'Height': 1 }").zAction(mongoImage => mongoImage.Image = DownloadPrint.LoadImage(mongoImage.Url)));
     MongoCommand.Find <MongoImage>("dl", "Images", "{}", sort: "{ 'Height': 1 }").zAction(mongoImage => mongoImage.Image = DownloadPrint.LoadImage(mongoImage.Url)).zView();
 }
示例#29
0
        public static void Test_RapideDdl_Images_01()
        {
            //pb.Data.Mongo.MongoCommand.FindAs("dl", "RapideDdl_Detail", "{}", fields: "{ 'download.images.Source': 1, 'download.images.ImageWidth': 1, 'download.images.ImageHeight': 1 }", limit: 5);
            //string query = "{ 'download.images.ImageHeight': { $gt: 100 } }";
            string query  = "{}";
            string fields = "{ 'download.images.Source': 1, 'download.images.ImageWidth': 1, 'download.images.ImageHeight': 1 }";
            int    limit  = 0;
            MongoCursor <BsonDocument> cursor = MongoCommand.GetDatabase(null, "dl").GetCollection("RapideDdl_Detail").zFind <BsonDocument>(new QueryDocument(BsonSerializer.Deserialize <BsonDocument>(query)));

            cursor.SetFields(new FieldsWrapper(BsonSerializer.Deserialize <BsonDocument>(fields)));
            if (limit != 0)
            {
                cursor.SetLimit(limit);
            }
            //DataTable dt = new DataTable();
            //dt.Load()
            //dt.LoadDataRow()
            //dt.ReadXml()
            //foreach (BsonDocument document in cursor)
            //{
            //    BsonArray array = document.GetElement("download").Value.AsBsonDocument.GetElement("images").Value.AsBsonArray;
            //    foreach (BsonValue value in array)
            //    {
            //        //Trace.WriteLine("BsonType : {0}", value.BsonType);
            //        Trace.WriteLine(value.ToJson());
            //    }
            //}
            //DataTable dt = Test_Bson.Test_Bson_f.BsonDocumentsToDataTable(cursor);
            DataTable dt = cursor.zToDataTable2();

            //dt.Select();
            //dt.Rows[0].Delete
            //foreach (DataRow row in dt.Rows)
            //{
            //    object imageHeightValue = row["download.images.ImageHeight"];
            //    //if (imageHeight is int && (int)imageHeight <= 60)
            //    //    row.Delete();
            //    int imageHeight;
            //    if (imageHeightValue is string && int.TryParse((string)imageHeightValue, out imageHeight))
            //    {
            //        if (imageHeight <= 60)
            //            row.Delete();
            //    }
            //}

            for (int i = 0; i < dt.Rows.Count;)
            {
                DataRow row = dt.Rows[i];
                object  imageHeightValue = row["download.images.ImageHeight"];
                //if (imageHeight is int && (int)imageHeight <= 60)
                //    row.Delete();
                int imageHeight;
                if (imageHeightValue is string && int.TryParse((string)imageHeightValue, out imageHeight) && imageHeight <= 60)
                {
                    //if (imageHeight <= 60)
                    row.Delete();
                }
                else
                {
                    i++;
                }
            }

            RunSource.CurrentRunSource.SetResult(dt);
        }
 /// <summary>
 /// 当前对象的MONGO命令
 /// </summary>
 /// <param name="cmd">命令对象</param>
 /// <returns></returns>
 public static CommandResult RunMongoCommandAtCurrentObj(MongoCommand cmd)
 {
     var Command = new CommandDocument { { cmd.CommandString, 1 } };
     CommandResult rtn = new CommandResult();
     switch (cmd.RunLevel)
     {
         case PathLv.CollectionLV:
             rtn = ExecuteMongoColCommand(cmd.CommandString, SystemManager.GetCurrentCollection());
             break;
         case PathLv.DatabaseLV:
             rtn =  ExecuteMongoDBCommand(Command, SystemManager.GetCurrentDataBase());
             break;
         case PathLv.ServerLV:
             rtn =  ExecuteMongoSvrCommand(Command, SystemManager.GetCurrentService());
             break;
         default:
             break;
     }
     return rtn;
 }
        /// <summary>
        /// 当前对象的MONGO命令
        /// </summary>
        /// <param name="cmd">命令对象</param>
        /// <returns></returns>
        public static void ExecuteMongoCommand(MongoCommand cmd)
        {
            var Command = new CommandDocument { { cmd.CommandString, 1 } };
            List<CommandResult> ResultCommandList = new List<CommandResult>();

            CommandResult rtn = new CommandResult();
            try
            {
                switch (cmd.RunLevel)
                {
                    case PathLv.CollectionLV:
                        rtn = ExecuteMongoColCommand(cmd.CommandString, SystemManager.GetCurrentCollection());
                        break;
                    case PathLv.DatabaseLV:
                        rtn = ExecuteMongoDBCommand(Command, SystemManager.GetCurrentDataBase());
                        break;
                    case PathLv.ServerLV:
                        rtn = ExecuteMongoSvrCommand(Command, SystemManager.GetCurrentServer());
                        break;
                    default:
                        break;
                }
                ResultCommandList.Add(rtn);
                MyMessageBox.ShowMessage(cmd.CommandString, cmd.CommandString + " Result", MongoDBHelper.ConvertCommandResultlstToString(ResultCommandList), true);
            }
            catch (System.IO.IOException ex)
            {
                MyMessageBox.ShowMessage(cmd.CommandString, "IOException,Try to set Socket TimeOut more long at connection config", ex.ToString(), true);
            }
            catch (Exception ex)
            {
                SystemManager.ExceptionDeal(ex, cmd.CommandString);
            }
        }