C# API

Schema

Attributes

BsonIgnoreExtraElements

Ignoring Extra Elements

BsonClassMap

When using interface, you have to register the Dependent class

public interface IDependent {
   int Index { get; set; }
   string Name { get; set; }
}

public class Person {
   int Id { get; set; }
   string Name {get; set; }
   IDependent[] Dependents { get; set; }
}
public class Dependent : IDependent {
    int Index {get; set; }
    string Name { get; set; }
}

BsonClassMap.RegisterClassMap<Dependent>();

Serialization and Deserialize

Serialization

BSON Serialization with MongoDB C# Driver

Deserialize a BsonDocument object back to class

There are three ways actually:

1.Specify type you want to load directly in FindAs<>

var myObj = _collection.FindAs<MyType>(_document);

2.Deserialize document viaBsonSerializer

BsonSerializer.Deserialize<MyType>(doc);

3.Map bson document manually to your class:

var myClass = new Mytype();
myClass.Name = bsonDoc["name"].AsString;

Get Raw Bson

db = get_data_db();
var collection = db.GetCollection<RawBsonDocument>(user_case_table);
var query = Query.EQ("_id", case_id);
var case_holder = collection.FindOne(query);
byte[] result = case_holder.ToBson();

Query

Query children

    public static CustomerGroup getCustomerGroupByEmail(string emailAddress)
    {
        MongoDatabase db = get_data_db();

        var collection = db.GetCollection<CustomerGroup>(customer_group_table);
        var memberQuery = Query<CustomerGroup.GroupMember>.EQ<string>(on => on.Email, emailAddress);
        var query = Query<CustomerGroup>.ElemMatch<CustomerGroup.GroupMember>(s => s.Members, builder => memberQuery);
        var entity = collection.Find(query).FirstOrDefault<CustomerGroup>();

        return entity;
    }

results matching ""

    No results matching ""