Store Json string as MongoDB array in C#

1.5k Views Asked by At

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

There are 1 best solutions below

0
On

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