Пример #1
0
        public object Update(object entity)
        {
            bool result = false;
            var  mlist  = entity as List <MedicationData>;

            try
            {
                using (MedicationMongoContext ctx = new MedicationMongoContext(ContractDBName))
                {
                    var bulk = ctx.Medications.Collection.InitializeUnorderedBulkOperation();

                    foreach (MEMedication fooDoc in ctx.Medications)
                    {
                        var update = new UpdateDocument {
                            { fooDoc.ToBsonDocument() }
                        };
                        update.Set("fmid", ObjectId.Parse(GetMedFamilyId(mlist, fooDoc.Id.ToString())));
                        bulk.Find(Query.EQ("_id", fooDoc.Id)).Upsert().UpdateOne(update);
                    }
                    BulkWriteResult bwr = bulk.Execute();

                    result = true;
                }
                return(result as object);
            }
            catch (Exception) { throw; }
        }
        public void RequestAddFriend(string ownerUsername, string friendUsername, string remark, string groupName, string message,
                                     RequestAddFriendUpdateFlags flags)
        {
            var db         = GetDatabase();
            var collection = db.GetCollection("friend_request");
            var queryDoc   = new QueryDocument();

            queryDoc.Add("OwnerUsername", ownerUsername.ToLower());
            queryDoc.Add("FriendUsername", friendUsername.ToLower());

            var cur = collection.Find(queryDoc);

            cur.Limit = 1;

            UpdateDocument updateDoc = new UpdateDocument();

            if (cur.Size() > 0)
            {
                foreach (var item in cur)
                {
                    updateDoc.Add(item);
                }
            }
            else
            {
                updateDoc.Add("OwnerUsername", ownerUsername.ToLower());
                updateDoc.Add("FriendUsername", friendUsername.ToLower());
            }

            if (flags == RequestAddFriendUpdateFlags.UpdateAll || (flags & RequestAddFriendUpdateFlags.UpdateRemark) ==
                RequestAddFriendUpdateFlags.UpdateRemark)
            {
                if (remark == null && updateDoc.Contains("Remark"))
                {
                    updateDoc.Remove("Remark");
                }
                else if (remark != null)
                {
                    updateDoc.Set("Remark", remark);
                }
            }
            if (flags == RequestAddFriendUpdateFlags.UpdateAll || (flags & RequestAddFriendUpdateFlags.UpdateMessage) ==
                RequestAddFriendUpdateFlags.UpdateMessage)
            {
                if (message == null && updateDoc.Contains("Message"))
                {
                    updateDoc.Remove("Message");
                }
                else if (message != null)
                {
                    updateDoc.Set("Message", message);
                }
            }
            if (flags == RequestAddFriendUpdateFlags.UpdateAll || (flags & RequestAddFriendUpdateFlags.UpdateGroup) ==
                RequestAddFriendUpdateFlags.UpdateGroup)
            {
                if (groupName == null && updateDoc.Contains("GroupName"))
                {
                    updateDoc.Remove("GroupName");
                }
                else if (groupName != null)
                {
                    updateDoc.Set("GroupName", groupName);
                }
            }
            updateDoc.Set("Time", DateTime.Now);
            collection.Update(queryDoc, updateDoc, UpdateFlags.Upsert);
        }