MongoUtility.Command.ElementHelper.GetLastParentDocument C# (CSharp) Method

GetLastParentDocument() public static method

Locate the Operation Place
public static GetLastParentDocument ( BsonDocument baseDoc, string elementPath, bool isGetLast ) : BsonValue
baseDoc BsonDocument
elementPath string
isGetLast bool T:GetOperationPlace F:GetOperationPlace Parent
return BsonValue
        public static BsonValue GetLastParentDocument(BsonDocument baseDoc, string elementPath, bool isGetLast)
        {
            BsonValue current = baseDoc;
            //JpCnWord[1]\Translations[ARRAY]\Translations[1]\Sentences[ARRAY]\Sentences[1]\Japanese:"ああいう文章はなかなか書けない"
            //1.将路径按照\分开
            var strPath = elementPath.Split(@"\".ToCharArray());
            //JpCnWord[1]                                    First
            //Translations[ARRAY]
            //Translations[1]
            //Sentences[ARRAY]
            //Sentences[1]
            //Japanese:"ああいう文章はなかなか書けない"        Last
            int deepLv;
            if (isGetLast)
            {
                deepLv = strPath.Length;
            }
            else
            {
                deepLv = strPath.Length - 1;
            }
            for (var i = 1; i < deepLv; i++)
            {
                var strTag = strPath[i];
                var isArray = false;
                if (strTag.EndsWith(ConstMgr.ArrayMark))
                {
                    //去除[Array]后缀
                    strTag = strTag.Substring(0, strTag.Length - ConstMgr.ArrayMark.Length);
                    isArray = true;
                }
                if (isArray)
                {
                    //这里的Array是指一个列表的上层节点,在BSON里面没有相应的对象,只是个逻辑概念
                    if (strTag == string.Empty)
                    {
                        //Array里面的Array,所以没有元素名称。
                        //TODO:正确做法是将元素的Index传入,这里暂时认为第一个数组就是目标数组
                        foreach (var item in current.AsBsonArray)
                        {
                            if (item.IsBsonArray)
                            {
                                current = item;
                            }
                        }
                    }
                    else
                    {
                        current = current.AsBsonDocument.GetValue(strTag).AsBsonArray;
                    }
                }
                else
                {
                    if (current.IsBsonArray)
                    {
                        //当前的如果是数组,获得当前下标。
                        int index =
                            Convert.ToInt16(strTag.Substring(strTag.IndexOf("[") + 1,
                                strTag.Length - strTag.IndexOf("[") - 2));
                        current = current.AsBsonArray[index - 1];
                    }
                    else
                    {
                        if (current.IsBsonDocument)
                        {
                            //如果当前还是一个文档的话
                            current = current.AsBsonDocument.GetValue(strTag);
                        }
                        else
                        {
                            //不应该会走到这个分支
                            return null;
                        }
                    }
                }
            }
            return current;
        }