如何在c#中插入嵌套集合到mongo db

时间:2013-05-07 11:35:30

标签: c# mongodb

我想使用c#

创建这样的集合
   Agent_name: {
    f: "vbv",
    l: "vbvbv"
    },
   "Agent_LoginSessionId" : "",
   "Agent_GroupCode" : "",
   "Agent_dtmCreated" : "",
   "Agent_dtmLastLogin" : "",
   "Agent_strIsActive" : ""

    stamps: {
     ins: "",
     upd: "",
     createUsr: "",
     updUsr: "",
     Ins_Ip: "",
     Upd_IP: ""

 }

我的C#代码是这样的:

MongoCollection<BsonDocument> Agents = mydb.GetCollection<BsonDocument>("Agents");
       BsonDocument collectionAgent = new BsonDocument {
                   { "Agent_Login", Aid },  
                   {"Agent_Pwd",Apwd},
                   {"Agent_Rpwd",Arpwd},

                   Agent_Name:     
                      { "f", Afname},
                      {"l", Alname} ,


                   {"Agent_LoginSessionId",""},
                   {"Agent_GroupCode",AgroupId},
                   {"Status",AStastus}
                  };
       Agents.Insert(collectionAgent)

IN C#怎么实现这个?它需要任何特定的驱动程序来安装。请提出建议?

1 个答案:

答案 0 :(得分:7)

要使用.NET驱动程序将文档插入MongoDB,您有两种选择来表示程序中的数据:

  1. 使用BsonDocument对象模型(如示例代码中所示)
  2. 使用C#类并让驱动程序将它们映射到BSON文档和从BSON文档映射
  3. 使用BsonDocument对象模型创建复杂的BSON文档可能会变得复杂,主要是因为涉及所有嵌套。以下是使用BsonDocument对象模型将该文档插入集合的方法:

    var bsonDocument = new BsonDocument
    {
        { "_id", ObjectId.GenerateNewId() },
        { "Agent_name", new BsonDocument
            {
                { "f", "vbv" },
                { "l", "vbvbv" }
            }
        },
        { "Agent_LoginSessionId", "" },
        { "Agent_GroupCode", "" },
        { "Agent_dtmCreated", "" },
        { "Agent_dtmLastLogin", "" },
        { "Agent_strIsActive", "" },
        { "stamps", new BsonDocument
            {
                { "ins", "" },
                { "upd", "" },
                { "createUsr", "" },
                { "updUsr", "" },
                { "Ins_Ip", "" },
                { "Upd_Ip", "" }
            }
        }
    };
    collection.Insert(bsonDocument);
    

    我在文档中添加了一个“_id”元素,因为所有MongoDB文档都有一个“_id”(如果您愿意,可以让服务器自动添加它)。

    另一种方法是定义一组C#类,驱动程序可以映射到BSON文档和从BSON文档映射。

    以下是使用C#类插入同一文档的方法:

    var agent = new Agent
    {
        Id = ObjectId.GenerateNewId(),
        Name = new AgentName { First = "vbv", Last = "vbvbv" },
        LoginSessionId = "",
        GroupCode = "",
        Created = "",
        LastLogin = "",
        IsActive = "",
        Stamps = new Stamps
        {
            Inserted = "",
            Updated = "",
            CreateUser = "",
            UpdateUser = "",
            InsertIPAddress = "",
            UpdateIPAddress = ""
        }
    };
    collection.Insert(agent);
    

    以下是C#类定义。我已经为属性使用了.NET命名约定,并使用[BsonElement(“...”)]属性来指定BSON文档中元素的相应名称:

    public class Agent
    {
        public ObjectId Id { get; set; }
        [BsonElement("Agent_name")]
        public AgentName Name { get; set; }
        [BsonElement("Agent_LoginSessionId")]
        public string LoginSessionId { get; set; }
        [BsonElement("Agent_GroupCode")]
        public string GroupCode { get; set; }
        [BsonElement("Agent_dtmCreated")]
        public string Created { get; set; }
        [BsonElement("Agent_dtmLastLogin")]
        public string LastLogin { get; set; }
        [BsonElement("Agent_strIsActive")]
        public string IsActive { get; set; }
        [BsonElement("stamps")]
        public Stamps Stamps { get; set; }
    }
    
    public class AgentName
    {
        [BsonElement("f")]
        public string First { get; set; }
        [BsonElement("l")]
        public string Last { get; set; }
    }
    
    public class Stamps
    {
        [BsonElement("ins")]
        public string Inserted { get; set; }
        [BsonElement("upd")]
        public string Updated { get; set; }
        [BsonElement("createUsr")]
        public string CreateUser { get; set; }
        [BsonElement("updUser")]
        public string UpdateUser { get; set; }
        [BsonElement("Ins_Ip")]
        public string InsertIPAddress { get; set; }
        [BsonElement("Upd_Ip")]
        public string UpdateIPAddress { get; set; }
    }