Пример #1
0
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="collName"></param>
        /// <param name="searchDoc">可为空,为空为插入</param>
        /// <param name="saveDoc"></param>
        /// <returns></returns>
        public InvokeResult Save(string collName, IMongoQuery query, BsonDocument saveDoc)
        {
            InvokeResult result = new InvokeResult()
            {
                Status = Status.Failed
            };

            try
            {
                MongoCollection <BsonDocument> entityColl = _mongoDatabase.GetCollection(collName);

                #region 保存到数据库中
                List <BsonDocument> entityList = new List <BsonDocument>();       //获取更新数据

                if (query != null)
                {
                    entityList = entityColl.Find(query).ToList();
                }
                else
                {
                    entityList = entityColl.FindAll().ToList();
                }

                using (_mongoServer.RequestStart(_mongoDatabase))
                {
                    //如果是一条记录,且与查询匹配,且有包含_id,则直接更新记录
                    if (entityList.Count == 1 && saveDoc.ContainsColumn("_id") && (entityList.FirstOrDefault().String("_id") == saveDoc.String("_id")))
                    {
                        entityColl.Save(saveDoc);
                    }
                    else                                //不包含_id,则逐条记录,逐个字段更新
                    {
                        foreach (var entity in entityList)
                        {
                            foreach (var temp in saveDoc.Elements)
                            {
                                entity[temp.Name] = temp.Value;
                            }

                            entityColl.Save(entity); //非安全模式,即无返回消息
                            //SafeModeResult safeResult = entityColl.Save(entity, SafeMode.True);
                            //if (safeResult.HasLastErrorMessage) throw new Exception(safeResult.LastErrorMessage);
                        }
                    }
                }
                #endregion

                result.Status = Status.Successful;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// 获取文本值
        /// </summary>
        /// <param name="bsonDoc"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static BsonArray Array(this BsonDocument bsonDoc, string name)
        {
            BsonArray dataArray = new BsonArray();

            if (bsonDoc.ContainsColumn(name) && !string.IsNullOrEmpty(bsonDoc.Text(name)))
            {
                dataArray = bsonDoc[name] as BsonArray;
            }
            return(dataArray);
        }
Пример #3
0
 /// <summary>
 /// 对数据进行解压缩保存
 /// </summary>
 /// <param name="bsonDoc"></param>
 /// <returns></returns>
 public BsonDocument LogDataMessageDePack(BsonDocument bsonDoc)
 {
     if (bsonDoc.Text("isMessagePack") == "1" && bsonDoc.ContainsColumn("runLog"))
     {
         var runLog = MessagePackSerializer.Deserialize <string>((byte[])bsonDoc["runLog"],
                                                                 MessagePack.Resolvers.ContractlessStandardResolver.Instance);
         bsonDoc.Set("runLog", runLog);
     }
     return(bsonDoc);
 }
Пример #4
0
 /// <summary>
 /// 对数据进行压缩保存
 /// </summary>
 /// <param name="bsonDoc"></param>
 /// <returns></returns>
 public BsonDocument LogDataMessagePack(BsonDocument bsonDoc)
 {
     //超过1000个字符串进行messagePack压缩
     if (bsonDoc.ContainsColumn("runLog") && bsonDoc.Text("runLog").Length >= 1000)
     {
         var runLog = MessagePackSerializer.Serialize(bsonDoc.Text("runLog"),
                                                      MessagePack.Resolvers.ContractlessStandardResolver.Instance);
         bsonDoc.Set("runLog", runLog);
         bsonDoc.Set("isMessagePack", "1");
     }
     return(bsonDoc);
 }
Пример #5
0
 /// <summary>
 /// 获取List<BsonDocument>
 /// </summary>
 /// <param name="bsonDoc"></param>
 /// <param name="name"></param>
 /// <returns></returns>
 public static List <BsonDocument> GetBsonDocumentList(this BsonDocument bsonDoc, string name)
 {
     try
     {
         if (bsonDoc.ContainsColumn(name))
         {
             return(MongoDB.Bson.Serialization.BsonSerializer.Deserialize <List <BsonDocument> >(
                        bsonDoc.String(name)));
         }
         else
         {
             return(new List <BsonDocument>());
         }
     }
     catch (Exception ex)
     {
         return(new List <BsonDocument>());
     }
 }