Exemplo n.º 1
0
        /// <summary>
        /// 根据文档对象组织树节点
        /// </summary>
        /// <param name="list"></param>
        /// <param name="pid"></param>
        /// <param name="doc"></param>
        protected void BuildTreeNode(List<MongoTreeNode> list, uint pid, BsonDocument doc)
        {
            foreach (var key in doc.Names)
            {
                var node = new MongoTreeNode
                {
                    ID = MongoConst.GetRandomId(),
                    PID = pid
                };

                var value = doc[key.ToString()];
                if (value is BsonDocument)
                {
                    node.Name = key.ToString();
                    list.Add(node);
                    BuildTreeNode(list, node.ID, value as BsonDocument);
                }
                else
                {
                    node.Name = string.Format("{0} : {1}", key, doc[key.ToString()]);
                    list.Add(node);
                }
            }
        }
Exemplo n.º 2
0
        private void GetFieldAndIndex(MongoDatabase db, uint tblid)
        {
            if (MongoObjects.ContainsKey(tblid))
            {
                var table = MongoObjects[tblid] as MongoCollectionModel;
                var watch = new Stopwatch();
                watch.Start();

                try
                {
                    #region 字段
                    //字段类型信息节点
                    var fieldNode = new MongoTreeNode
                    {
                        ID = MongoConst.GetRandomId(),
                        PID = table.ID,
                        Name = "表信息",
                        Type = MongoTreeNodeType.TableFiller
                    };
                    TreeNodes.Add(fieldNode);

                    //字段节点
                    var doc = db.GetCollection(table.Name).FindOne();
                    if (doc != null)
                    {
                        foreach (var item in doc.Names)
                        {
                            var field = new MongoFieldModel
                            {
                                ID = MongoConst.GetRandomId(),
                                Name = item.ToString(),
                                Type = doc[item].BsonType
                            };

                            TreeNodes.Add(new MongoTreeNode
                            {
                                ID = field.ID,
                                PID = fieldNode.ID,
                                Name = field.Name + string.Format(" ({0})", field.Type),
                                Type = MongoTreeNodeType.Field
                            });

                            MongoObjects.Add(field.ID, field);
                        }
                    }
                    #endregion

                    #region 索引
                    //索引类型信息节点
                    var indexNode = new MongoTreeNode
                    {
                        ID = MongoConst.GetRandomId(),
                        PID = table.ID,
                        Name = "索引",
                        Type = MongoTreeNodeType.IndexFiller
                    };
                    TreeNodes.Add(indexNode);

                    //索引节点
                    var indexes = db.GetCollection(MongoConst.IndexTableName).Find(new QueryDocument { { "ns", table.Namespace } });
                    if (indexes != null)
                    {
                        foreach (var idx in indexes.ToList())
                        {
                            var index = new MongoIndexModel
                            {
                                ID = MongoConst.GetRandomId(),
                                Name = idx["name"].AsString,
                                Namespace = idx["ns"].AsString,
                                Unique = idx.Contains("unique") ? (idx["unique"].AsDouble == 1.0 ? true : false) : false,
                                Keys = new List<MongoIndexKey>()
                            };
                            var docFields = idx["key"].AsBsonDocument;
                            foreach (var key in docFields.Names)
                            {
                                index.Keys.Add(new MongoIndexKey
                                {
                                    FieldName = key.ToString(),
                                    OrderType = int.Parse(docFields[key.ToString()].ToString()) == 1 ? MongoIndexOrderType.Ascending : MongoIndexOrderType.Descending
                                });
                            }

                            TreeNodes.Add(new MongoTreeNode
                            {
                                ID = index.ID,
                                PID = indexNode.ID,
                                Name = index.Name,
                                Type = MongoTreeNodeType.Index
                            });

                            MongoObjects.Add(index.ID, index);
                        }
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    LogManager.GetLogger("MongoContext").Error("获取字段和索引信息时出错", ex);
                }

                LogManager.GetLogger("InfoLog").Info(string.Format("获取服务表{0}的所有字段和索引对象共花费了大约{1}毫秒的时间", table.Name, watch.ElapsedMilliseconds));
                watch.Stop();
            }
        }