0

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

2
  • So what if we cannot cast the Event_meta data to array format key-value pair ? Commented Sep 28, 2018 at 8:49
  • BsonSerializer.Deserialize<BsonArray>(json); this method returned bson array. or you can try add Dictionary<string,object>[] property and save to mongodb. Commented Sep 28, 2018 at 9:05

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.