将Json字符串存储为C#中的MongoDB数组 [英] Store Json string as MongoDB array in C#

查看:268
本文介绍了将Json字符串存储为C#中的MongoDB数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个JSON字符串以数组格式存储在MongoDB中.这是我的C#代码,用于将通知存储到MongoDB中,此处的"Event_meta"字段以JSON字符串格式发送到MongoDB.

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)
        {


        }

    }

"eventmsg.Event_meta"是动态类型."Event_meta"值以jsonstring的形式存储在MongoDB中.但是我想将其存储为Array格式.

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"..."

感谢您的帮助

推荐答案

使您的Event_meta字段类型进入BsonDocument并反序列化为BsonDocument. 或将您的Notifications类设置为映射器类,例如 Mongo Doc

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

这篇关于将Json字符串存储为C#中的MongoDB数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆