1

I want to create collection like this using 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: ""

 }

And my C# code is like this:

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# how to achieve this? It need any specific driver to install. please suggest something?

1 Answer 1

8

To insert documents into MongoDB using the .NET driver you have two choices for how to represent the data in your program:

  1. Use the BsonDocument object model (as in your sample code)
  2. Use C# classes and let the driver map them to and from BSON documents

Creating complex BSON documents using the BsonDocument object model can get complicated, mostly because of all the nesting involved. Here's how you could insert that document into a collection using the BsonDocument object model:

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

I've added an "_id" element to the document because all MongoDB documents will have an "_id" (you could let the server add it automatically if you prefer).

Another alternative is to define a set of C# classes that the driver can map to and from BSON documents.

Here's how you could insert the same document using C# classes:

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

And here are the C# class definitions. I've used .NET naming conventions for the properties and used the [BsonElement("...")] attribute to specify the corresponding name of the element in the BSON document:

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; }
}
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.