Store Json string as MongoDB array in C#

随声附和 提交于 2019-12-02 20:25:49

问题


I want to store a JSON string in an array format in MongoDB.This is my C# code for storing notification into MongoDB, here the field "Event_meta" field sending as a JSON string format to MongoDB.

public async Task Handle(TaskPropertyUpdatedEvent eventmsg)
        {
        try
        {
            var meta = JsonConvert.SerializeObject(eventmsg.Event_meta);
            var notificationEvent = new Notifications()
            {
                Content = eventmsg.Content,
                Type = "TaskEvent",
                UserId = eventmsg.UpdatedById,
                EntityId = eventmsg.TaskId.ToString(),
                AddedDate = eventmsg.UpdatedDate,
                Active = true,
                ShowNotification = false,
                InternalEvent = true,
                UserName = eventmsg.UpdatedByName,
                Event_type = eventmsg.Event_type,
                Event_meta = meta

            };
            var result = await _eventRepository.AddEventAsync(notificationEvent);              
        }

        catch (Exception ex)
        {


        }

    }

The "eventmsg.Event_meta" is a dynamic type."Event_meta" value storing in MongoDB as jsonstring.But i want store this as an Array format.

Event_meta :"[{"action":"added","propertyId":"e7b6df49-7ea5-422f-aa81- 
1d273d69da21"..."

Any help is appreciated


回答1:


Make your Event_meta field type into BsonDocument and deserialize as BsonDocument. Or make your Notifications class as mapper class like Mongo Doc



来源:https://stackoverflow.com/questions/52550759/store-json-string-as-mongodb-array-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!