SlideShare a Scribd company logo
1 of 50
Download to read offline
Open	
  source,	
  high	
  performance	
  database	
  


What’s New in the C#/.NET Driver
            Robert Stam
      Software Engineer, 10gen

                                                       July	
  19,	
  2012	
  

                                                                                 1
•  The	
  C#/.NET	
  driver	
  
       –  Is	
  wriFen	
  in	
  C#	
  
       –  Can	
  be	
  called	
  from	
  other	
  .NET	
  languages	
  
•  Where	
  to	
  find	
  more	
  informaIon:	
  
       –  hFp://www.mongodb.org/display/DOCS/CSharp+Language+Center	
  
	
  




                                                                          2
•  Recent	
  versions	
  
    –  1.4	
   	
     	
  Introduced	
  support	
  for	
  LINQ	
  queries	
  
    –  1.4.1	
        	
  Added	
  support	
  for	
  addiIonal	
  LINQ	
  queries	
  
    –  1.4.2	
        	
  A	
  few	
  bug	
  fixes	
  
    –  1.5	
   	
     	
  SerializaIon	
  changes,	
  new	
  Query	
  builders	
  
•  Upcoming	
  version	
  
    –  1.6	
   	
     	
  Support	
  for	
  new	
  server	
  2.2	
  features	
  




                                                                                        3
•  Today’s	
  topics:	
  
    –  SerializaIon	
  (POCOs)	
  
    –  Handling	
  schema	
  evoluIon	
  
    –  The	
  new	
  Query	
  builders	
  
    –  LINQ	
  queries	
  
    –  AuthenIcaIon	
  




                                             4
•  Documents	
  are	
  stored	
  as	
  BSON	
  in	
  the	
  database	
  
•  When	
  you	
  read	
  a	
  document	
  from	
  the	
  database	
  what	
  
   do	
  you	
  get	
  back	
  in	
  your	
  C#	
  program?	
  
•  You	
  can	
  choose	
  to	
  get	
  back	
  either:	
  
    –  A	
  BsonDocument	
  
    –  A	
  plain	
  old	
  C#	
  object	
  (POCO)	
  that	
  you	
  defined	
  
•  SerializaIon	
  is	
  the	
  process	
  by	
  which	
  a	
  POCO	
  instance	
  
   is	
  transformed	
  to	
  a	
  BSON	
  document	
  and	
  back	
  



                                                                                      5
•  An	
  in	
  memory	
  representaIon	
  of	
  a	
  BSON	
  document	
  
•  It’s	
  not	
  the	
  original	
  binary	
  BSON,	
  it’s	
  the	
  result	
  of	
  
   decoding	
  the	
  binary	
  BSON	
  
•  Very	
  similar	
  to	
  a	
  DicIonary<string,	
  BsonValue>	
  
•  Important	
  classes:	
  
     –  BsonValue	
  (abstract	
  base	
  class)	
  
     –  BsonInt32,	
  BsonInt64,	
  BsonString,	
  BsonDateTime,	
  …	
  
     –  BsonDocument,	
  BsonArray	
  
•  More	
  informaIon:	
  
   hFp://www.mongodb.org/display/DOCS/CSharp+Driver
   +Tutorial#CSharpDriverTutorial-­‐BsonValueandsubclasses	
  
                                                                                          6
•  Very	
  likely	
  you	
  will	
  want	
  to	
  use	
  your	
  own	
  domain	
  
   classes	
  instead	
  of	
  BsonDocument	
  
•  SerializaIon	
  makes	
  that	
  possible	
  
•  Sample	
  code:	
  
var collection = database.GetCollection<Employee>("employees");
var employee = collection.FindOne(
    Query<Employee>.EQ(e => e.EmployeeNumber, 1234));
employee.Salary += 1000;
collection.Save(employee);




                                                                                     7
•  The	
  driver	
  users	
  serializers	
  to	
  convert	
  POCOs	
  to	
  and	
  
   from	
  BSON	
  documents	
  
•  A	
  serializer	
  is	
  a	
  class	
  that	
  implements	
  IBsonSerializer	
  
•  A	
  serializer	
  is	
  registered	
  by	
  calling	
  
   BsonSerializer.RegisterSerializer	
  
•  The	
  driver	
  provides	
  many	
  serializers	
  for	
  
   common	
  .NET	
  types	
  
•  You	
  can	
  write	
  your	
  own	
  if	
  you	
  need	
  to	
  
•  Class	
  map	
  based	
  serializaIon	
  works	
  automaIcally	
  for	
  
   your	
  POCOs	
  if	
  you	
  follow	
  a	
  few	
  simple	
  rules	
  
                                                                                      8
•  If	
  your	
  POCOs	
  follow	
  these	
  rules	
  they	
  can	
  be	
  
   serialized	
  automaIcally	
  by	
  the	
  C#/.NET	
  driver:	
  
    –  Has	
  a	
  public	
  no-­‐argument	
  constructor	
  
    –  Has	
  a	
  public	
  get/set	
  property	
  for	
  each	
  value	
  that	
  you	
  want	
  
       to	
  have	
  serialized	
  
•  In	
  most	
  cases	
  automaIc	
  class	
  map	
  based	
  
   serializaIon	
  requires	
  you	
  to	
  do:	
  NOTHING!	
  
•  You	
  only	
  need	
  to	
  do	
  something	
  when	
  you	
  want	
  to	
  
   override	
  the	
  automaIc	
  serializaIon	
  


                                                                                                      9
•  One	
  way	
  to	
  configure	
  automaIc	
  serializaIon	
  is	
  by	
  
   annotaIng	
  your	
  class	
  with	
  aFributes	
  
[BsonIgnoreExtraElements]
public class Employee {
    [BsonId]
    public ObjectId EmployeeNumber { get; set; }
    [BsonElement("nm")]
    public string Name { get; set; }
    [BsonDateTimeOptions(DateOnly = true)]
    public DateTime DateOfBirth { get; set; }
    [BsonRepresentation(BsonType.Int64)]
    public int Salary { get; set; }
}



                                                                              10
•  If	
  you	
  want	
  your	
  domain	
  classes	
  to	
  be	
  independent	
  of	
  
   your	
  persistence	
  layer	
  you	
  can	
  configure	
  serializaIon	
  
   in	
  code	
  instead	
  of	
  using	
  aFributes	
  
BsonClassMap.RegisterClassMap<Employee>(cm => {
    cm.AutoMap();
    cm.SetIgnoreExtraElements(true);
    cm.SetIdMember(cm.GetMemberMap(c => c.EmployeeNumber));
    cm.GetMemberMap(c => c.Name).SetElementName("nm");
    cm.GetMemberMap(c => c.DateOfBirth)
        .SetSerializationOptions(
            DateTimeSerializationOptions.DateOnlyInstance);
    cm.GetMemberMap(c => c.Salary)
        .SetRepresentation(BsonType.Int64);
});

                                                                                         11
•  Common	
  changes	
  to	
  your	
  schema	
  
    –  You	
  added	
  a	
  new	
  property	
  
    –  You	
  removed	
  an	
  exisIng	
  property	
  
    –  You	
  renamed	
  an	
  exisIng	
  property	
  
    –  You	
  changed	
  the	
  representaIon	
  of	
  an	
  exisIng	
  property	
  
    –  You	
  changed	
  the	
  type	
  of	
  an	
  exisIng	
  property	
  
•  MigraIon	
  strategies	
  
    –  All	
  at	
  once	
  using	
  an	
  upgrade	
  script	
  (much	
  easier)	
  
    –  Incremental	
  



                                                                                       12
•  ExisIng	
  documents	
  don't	
  have	
  a	
  value	
  for	
  the	
  new	
  
   property,	
  new	
  documents	
  automaIcally	
  will	
  
•  When	
  you	
  deserialize	
  a	
  document	
  that	
  doesn't	
  have	
  
   that	
  element,	
  the	
  property	
  in	
  your	
  class	
  will	
  be	
  null	
  
   (or	
  zero)	
  
•  You	
  can	
  provide	
  a	
  default	
  value	
  for	
  missing	
  elements	
  
   if	
  you	
  want	
  
    [BsonDefaultValue(Status.Active)]
    public Status EmployeeStatus { get; set; }
// or
    cm.GetMemberMap(c => c.EmployeeStatus)
        .SetDefaultValue(Status.Active);
                                                                                          13
•  ExisIng	
  documents	
  sIll	
  have	
  an	
  element	
  for	
  the	
  
   property	
  that	
  you	
  removed	
  
•  An	
  excepIon	
  will	
  be	
  thrown	
  when	
  a	
  document	
  
   containing	
  the	
  removed	
  property	
  is	
  deserialized	
  
   because	
  we	
  don't	
  know	
  what	
  to	
  do	
  with	
  the	
  
   unexpected	
  element	
  
•  You	
  could	
  handle	
  this	
  in	
  two	
  ways	
  
    –  Ignore	
  the	
  extra	
  element	
  
    –  Add	
  an	
  ExtraElements	
  property	
  to	
  your	
  class	
  



                                                                             14
•  If	
  you	
  just	
  want	
  to	
  rename	
  the	
  property	
  in	
  your	
  code	
  
   but	
  keep	
  the	
  same	
  element	
  name	
  in	
  the	
  database	
  
   use	
  the	
  [BsonElement]	
  aFribute	
  or	
  the	
  
   SetElementName	
  method	
  
•  If	
  you	
  use	
  a	
  migraIon	
  script	
  to	
  update	
  the	
  enIre	
  
   collecIon	
  at	
  once	
  simply	
  change	
  the	
  name	
  of	
  the	
  
   element	
  in	
  the	
  database	
  
•  Otherwise,	
  consider	
  wriIng	
  a	
  custom	
  serializer	
  or	
  
   perhaps	
  just	
  implemenIng	
  ISupportIniIalize	
  


                                                                                            15
public class C : ISupportInitialize {
    [BsonExtraElements]
    public BsonDocument ExtraElements;

    public   void BeginInit() { }
    public   void EndInit() {
        //   check ExtraElements for elements with old names
        //   and load them into the appropriate property
        //   also clear them out of ExtraElements so they won't
        //   be saved back to the document again
    }
}




                                                                  16
•  If	
  the	
  C#	
  type	
  didn't	
  change	
  and	
  the	
  two	
  
   representaIons	
  are	
  compaIble	
  you	
  don't	
  need	
  to	
  do	
  
   anything.	
  Old	
  documents	
  can	
  sIll	
  be	
  read	
  and	
  new	
  
   documents	
  will	
  be	
  wriFen	
  with	
  the	
  new	
  
   representaIon	
  
•  Otherwise,	
  you	
  will	
  either	
  have	
  to	
  write	
  a	
  custom	
  
   serializer	
  or	
  a	
  migraIon	
  script	
  




                                                                                   17
•  If	
  the	
  new	
  type	
  is	
  compaIble	
  with	
  the	
  old	
  type	
  (e.g.	
  
   you	
  changed	
  from	
  Int32	
  to	
  Int64)	
  you	
  don't	
  need	
  to	
  
   do	
  anything	
  
•  Otherwise,	
  you	
  will	
  have	
  to	
  write	
  a	
  migraIon	
  script	
  
   or	
  a	
  custom	
  serializer	
  




                                                                                            18
•  What	
  is	
  a	
  query?	
  
     –  Anything	
  that	
  implements	
  IMongoQuery	
  
     –  IMongoQuery	
  has	
  an	
  implied	
  contract:	
  that	
  when	
  the	
  
        object	
  is	
  serialized	
  to	
  a	
  BSON	
  document	
  it	
  will	
  be	
  a	
  valid	
  
        MongoDB	
  query	
  
•  Ways	
  to	
  create	
  query:	
  
     –  new	
  QueryDocument	
  {	
  …	
  }	
  
     –  new	
  QueryWrapper(object	
  query)	
  
     –  Use	
  the	
  untyped	
  query	
  builder	
  
     –  Use	
  the	
  typed	
  query	
  builder	
  


                                                                                                          19
•  Version	
  1.5	
  introduced	
  a	
  new	
  query	
  builder	
  
•  What	
  was	
  wrong	
  with	
  the	
  old	
  query	
  builder?	
  
    –  It	
  exposed	
  too	
  many	
  of	
  the	
  idiosyncrasies	
  of	
  the	
  naIve	
  
       MongoDB	
  query	
  language	
  (implied	
  and,	
  unusual	
  
       restricIons	
  of	
  not,	
  etc…)	
  
    –  It	
  required	
  some	
  helper	
  classes	
  (QueryComplete,	
  
       QueryNot,	
  etc…)	
  that	
  were	
  intended	
  to	
  be	
  internal	
  but	
  yet	
  
       had	
  to	
  be	
  public	
  or	
  you	
  couldn't	
  write	
  a	
  query	
  
    –  The	
  query	
  language	
  rules	
  that	
  the	
  helper	
  classes	
  were	
  
       aFempIng	
  to	
  enforce	
  were	
  changing	
  from	
  version	
  to	
  
       version	
  of	
  the	
  server	
  but	
  the	
  driver	
  couldn't	
  adapt	
  
       gracefully	
  because	
  the	
  rules	
  were	
  encoded	
  in	
  source	
  code	
  
                                                                                                  20
•  Works	
  at	
  a	
  slightly	
  higher	
  level	
  (e.g.,	
  requires	
  
   Query.And	
  instead	
  of	
  implied	
  and)	
  
•  Much	
  simpler	
  API	
  (and	
  no	
  helper	
  classes	
  that	
  leak	
  
   out	
  into	
  the	
  public	
  API)	
  
•  Mostly	
  compaIble	
  with	
  the	
  old	
  query	
  builder	
  
   (specially	
  for	
  simple	
  queries)	
  
•  Easier	
  to	
  maintain	
  from	
  version	
  to	
  version	
  of	
  the	
  
   server	
  



                                                                                   21
•  Yes!	
  The	
  old	
  query	
  builder	
  will	
  be	
  removed	
  in	
  a	
  
   future	
  release	
  
•  For	
  the	
  Ime	
  being,	
  you	
  can	
  conInue	
  using	
  the	
  old	
  
   query	
  builder	
  without	
  changing	
  your	
  source	
  code	
  by	
  
   adding	
  the	
  following	
  lines	
  to	
  the	
  top	
  of	
  your	
  source	
  
   file:	
  
#pragma warning disable 618 // DeprecatedQuery is marked Obsolete
using Query = MongoDB.Driver.Builder.DeprecatedQuery;




                                                                                         22
•  There	
  is	
  a	
  method	
  in	
  the	
  untyped	
  query	
  builder	
  for	
  
   every	
  query	
  operator	
  in	
  the	
  MongoDB	
  query	
  
   language	
  
Query.EQ("_id", 1234)
Query.EQ("nm", "John Doe")
Query.NE("EmployeeStatus", 1) // assumes Status.Active == 1
Query.GT("Salary", 100000)

// are equivalent to
new QueryDocument("_id", 1234)
new QueryDocument("nm", "John Doe")
new QueryDocument("EmployeeStatus", new BsonDocument("$ne", 1))
new QueryDocument("Salary", new BsonDocument("$gt", 100000))


                                                                                       23
•  You	
  have	
  to	
  know	
  the	
  element	
  name	
  
    –  What	
  if	
  you	
  use	
  [BsonElement]	
  to	
  change	
  the	
  element	
  
       name?	
  
    –  What	
  if	
  you	
  mistype	
  the	
  element	
  name?	
  
•  You	
  have	
  to	
  correctly	
  serialize	
  any	
  values	
  yourself	
  
    –  What	
  if	
  you	
  serialize	
  the	
  value	
  wrong?	
  
    –  What	
  if	
  you	
  don't	
  even	
  know	
  how	
  to	
  serialize	
  the	
  value?	
  




                                                                                                   24
•  The	
  typed	
  builder	
  has	
  the	
  same	
  methods	
  as	
  the	
  
   untyped	
  builder	
  but	
  is	
  type	
  aware	
  and	
  type	
  safe	
  
Query<Employee>.EQ(d       =>   d.EmployeeNumber, 1234)
Query<Employee>.EQ(d       =>   d.Name, "John Doe")
Query<Employee>.NE(d       =>   d.EmployeeStatus, Status.Active)
Query<Employee>.GT(d       =>   d.Salary, 100000)

// also equivalent to
new QueryDocument("_id", 1234)
new QueryDocument("nm", "John Doe")
new QueryDocument("EmployeeStatus", new BsonDocument("$ne", 1))
new QueryDocument("Salary", new BsonDocument("$gt", 100000))




                                                                                 25
•  The	
  typed	
  query	
  builder	
  also	
  lets	
  you	
  write	
  the	
  
   predicate	
  in	
  C#	
  and	
  it	
  will	
  be	
  translated	
  to	
  an	
  
   equivalent	
  MongoDB	
  query	
  
Query<Employee>.Where(d          =>   d.EmployeeNumber == 1234)
Query<Employee>.Where(d          =>   d.Name == "John Doe")
Query<Employee>.Where(d          =>   d.EmployeeStatus != Status.Active)
Query<Employee>.Where(d          =>   d.Salary > 100000)

// still equivalent to
new QueryDocument("_id", 1234)
new QueryDocument("nm", "John Doe")
new QueryDocument("EmployeeStatus", new BsonDocument("$ne", 1))
new QueryDocument("Salary", new BsonDocument("$gt", 100000))


                                                                                    26
var query = Query.GTE("x", 1).LTE(3);
// becomes
var query = Query.And(Query.GTE("x", 1), Query.LTE("x", 3));

var query = Query.Not("x").GT(1);
// becomes
var query = Query.Not(Query.GT("x", 1));

var query = Query.Exists("x", true);
// becomes
var query = Query.Exists("x");

var query = Query.Exists("x", false);
// becomes
var query = Query.NotExists("x");



                                                               27
•  LINQ	
  is	
  supported	
  for	
  queries	
  only	
  (although	
  
   Query<T>.Where	
  supports	
  wriIng	
  predicates	
  in	
  C#	
  
   that	
  can	
  be	
  used	
  with	
  Update)	
  
•  You	
  opt-­‐in	
  to	
  LINQ	
  using	
  AsQueryable()	
  
var collection = database.GetCollection<Employee>("employees");
var query = from e in collection.AsQueryable()
    where e.Name == "John Doe" && e.Salary >= 100000
    select e;

foreach (var employee in query) {
    // process employee
}



                                                                        28
•  C#	
  compiler	
  creates	
  an	
  Expression	
  tree	
  
•  C#/.NET	
  driver	
  translates	
  the	
  Expression	
  tree	
  to	
  an	
  
   equivalent	
  MongoDB	
  query	
  at	
  run	
  Ime	
  
•  Requirements	
  
    –  For	
  each	
  C#	
  property	
  referenced	
  in	
  the	
  LINQ	
  query	
  the	
  
       driver	
  has	
  to	
  be	
  able	
  to	
  figure	
  out	
  the	
  matching	
  element	
  
       name	
  in	
  the	
  BSON	
  document	
  (using	
  doFed	
  names	
  for	
  
       nested	
  elements)	
  
    –  For	
  each	
  test	
  using	
  those	
  C#	
  properIes	
  the	
  driver	
  has	
  to	
  
       be	
  be	
  able	
  to	
  translate	
  the	
  test	
  into	
  an	
  equivalent	
  
       MongoDB	
  query	
  operator	
  

                                                                                                    29
•  The	
  primary	
  goal	
  is:	
  we	
  will	
  only	
  support	
  LINQ	
  
   queries	
  that	
  have	
  a	
  reasonable	
  translaIon	
  to	
  an	
  
   equivalent	
  MongoDB	
  query	
  
•  The	
  reason:	
  we	
  want	
  to	
  ensure	
  predictable	
  
   performance	
  from	
  LINQ	
  queries	
  (no	
  black	
  magic,	
  no	
  
   surprises)	
  
•  So,	
  you	
  will	
  not	
  find:	
  
    –  Any	
  hidden	
  map/reduce	
  or	
  Javascript	
  tricks	
  
    –  Any	
  hidden	
  client	
  side	
  processing	
  
•  Online	
  LINQ	
  tutorial	
  at:	
  
   hFp://www.mongodb.org/display/DOCS/CSharp+Driver+LINQ+Tutorial	
  
                                                                                30
var query = from e in collection.AsQueryable<Employee>()
    where e.EmployeeStatus == Status.Active
    select e;
// translates to (assuming enum value for Active is 1):
{ EmployeeStatus : 1 }

var query = from e in collection.AsQueryable<Employee>()
    where e.EmployeeStatus != Status.Active
    select e;
// translates to:
{ EmployeeStatus : { $ne : 1 } }




                                                           31
var query = from e in collection.AsQueryable<Employee>()
    where
        e.EmployeeStatus == Status.Active &&
        e.Salary > 100000
    select e;
// translates to:
{ EmployeeStatus : 1, Salary : { $gt : 100000 } }




                                                           32
var query = from e in collection.AsQueryable<Employee>()
    where
        e.EmployeeStatus == Status.Active ||
        e.Salary > 100000
    select e;
// translates to:
{ $or : [
    { EmployeeStatus : 1 },
    { Salary : { $gt : 100000 } }
]}




                                                           33
var query = from e in collection.AsQueryable<Employee>()
    where e.Name.Contains("oh")
    select e;
// translates to:
{ nm: /oh/s }

var query = from e in collection.AsQueryable<Employee>()
    where e.Name.StartsWith("John")
    select e;
// translates to:
{ nm: /^John/s }




                                                           34
var query = from e in collection.AsQueryable<Employee>()
    where e.Name.Length == 4
    select e;
// translates to:
{ nm: /^.{4}$/s }

var query = from e in collection.AsQueryable<Employee>()
    where string.IsNullOrEmpty(e.Name)
    select e;
// translates to:
{ $or : [ { nm: { $type : 10 } }, { nm: "" } ] }




                                                           35
var query = from e in collection.AsQueryable<Employee>()
    where e.Name.ToLower() == "john macadam"
    select e;
// translates to:
{ nm: /^john macadam$/is }




                                                           36
var query = from e in collection.AsQueryable<Employee>()
    where e.Skills[0] == "Java"
    select e;
// translates to:
{ "Skills.0" : "Java" }

var query = from e in collection.AsQueryable<Employee>()
    where e.Skills.Length == 3
    select e;
// translates to:
{ Skills : { $size : 3 } }




                                                           37
var query = from e in collection.AsQueryable<Employee>()
    where e.Address.City == "Hoboken"
    select e;
// translates to:
{ "Address.City" : "Hoboken" }




                                                           38
var states = new [] { "NJ", "NY", "PA" };
var query = from e in collection.AsQueryable<Employee>()
    where states.Contains(e.Address.State)
    select e;
// translates to:
{ "Address.State" : { $in : [ "NJ", "NY", "PA" ] } }

// alternative syntax using C#/.NET driver "In" method
var query = from e in collection.AsQueryable<Employee>()
    where e.Address.State.In(states)
    select e;




                                                           39
var desiredSkills = new [] { "Java", "C#" };
var query = from e in collection.AsQueryable<Employee>()
    where e.Skills.ContainsAny(desiredSkills)
    select e;
// translates to:
{ "Skills" : { $in : [ "Java", "C#" ] } }

var query = from e in collection.AsQueryable<Employee>()
    where e.Skills.ContainsAll(desiredSkills)
    select e;
// translates to:
{ "Skills" : { $all : [ "Java", "C#" ] } }

// note: ContainsAny and ContainsAll are defined by the C#/.NET
   driver and are not part of standard LINQ



                                                                  40
var query = from e in collection.AsQueryable<Employee>()
    where
        e.Addresses.Any(a =>
            a.City == "Hoboken" &&
            a.State == "NJ")
    select e;
// translates to:
{ "Addresses" : { $elemMatch :
    { City : "Hoboken", State : "NJ" } } }




                                                           41
•  You	
  can	
  "Inject"	
  naIve	
  MongoDB	
  queries	
  into	
  a	
  LINQ	
  
   query	
  if	
  you	
  need	
  to	
  include	
  a	
  test	
  that	
  LINQ	
  doesn't	
  
   support,	
  without	
  giving	
  up	
  LINQ	
  enIrely	
  
var query = from e in collection.AsQueryable()
    where
        e.Salary > 50000 &&
        Query.NotExists("EmployeeStatus").Inject()
    select e;
// translates to:
{
    Salary : { $gt : 50000 },
    EmployeeStatus : { $exists : false }
}


                                                                                             42
•  Turn	
  on	
  authenIcaIon	
  using	
  mongod	
  -­‐-­‐auth	
  
•  AuthenIcaIon	
  is	
  at	
  the	
  database	
  level	
  
•  Add	
  a	
  username/password	
  to	
  each	
  database	
  that	
  
   you	
  need	
  to	
  access	
  
•  AuthenIcaIng	
  against	
  a	
  username/password	
  in	
  the	
  
   admin	
  database	
  is	
  like	
  root	
  access,	
  it	
  gives	
  you	
  access	
  
   to	
  all	
  databases	
  




                                                                                            43
•  If	
  you	
  are	
  using	
  the	
  same	
  credenIals	
  with	
  all	
  
   databases	
  you	
  can	
  simply	
  set	
  the	
  default	
  credenIals	
  
	
  
// on the connection string
var connectionString = "mongodb://user:pwd@localhost/?safe=true";
var server = MongoServer.Create(connectionString);

// in code
var settings = new MongoServerSettings() {
    DefaultCredentials = new MongoCredentials("user", "pwd"),
    SafeMode = SafeMode.True
};
var server = MongoServer.Create(settings);




                                                                                  44
•  If	
  you	
  are	
  using	
  different	
  credenIals	
  for	
  each	
  
   database	
  you	
  can	
  use	
  a	
  credenIals	
  store	
  to	
  hold	
  all	
  
   the	
  credenIals	
  (in	
  code	
  only,	
  not	
  supported	
  on	
  the	
  
   connecIon	
  string)	
  
var credentialsStore = new MongoCredentialsStore();
credentialsStore.AddCredentials(
    "hr", new MongoCredentials("user1", "pwd1"));
credentialsStore.AddCredentials(
    "inventory", new MongoCredentials("user2", "pwd2"));
var settings = new MongoServerSettings {
    CredentialsStore = credentialsStore,
    SafeMode = SafeMode.True
};
var server = MongoServer.Create(settings);
                                                                                        45
•  You	
  can	
  also	
  postpone	
  providing	
  the	
  credenIals	
  unIl	
  
   you	
  call	
  GetDatabase	
  
var credentials = new MongoCredentials("user", "pwd");
var database = server.GetDatabase("hr", credentials);




                                                                                  46
•  To	
  authenIcate	
  using	
  the	
  admin	
  database	
  you	
  have	
  to	
  
   flag	
  the	
  credenIals	
  as	
  being	
  admin	
  credenIals	
  
•  You	
  can	
  either	
  add	
  "(admin)"	
  to	
  the	
  end	
  of	
  the	
  user	
  
   name	
  or	
  set	
  the	
  Admin	
  flag	
  to	
  true	
  
var cs = "mongodb://user(admin):pwd@localhost/?safe=true";
var server = MongoServer.Create(cs);

var adminCredentials = new MongoCredentials("user", "pwd", true);
var settings = new MongoServerSettings {
    DefaultCredentials = adminCredentials,
    SafeMode = SafeMode.True
};
var server = MongoServer.Create(settings);

                                                                                       47
•  You	
  have	
  to	
  provide	
  admin	
  credenIals	
  to	
  run	
  a	
  
   command	
  against	
  the	
  admin	
  database	
  
var   adminCredentials = new MongoCredentials("user", "pwd", true);
var   adminDatabase = server.GetDatabase("admin", adminCredentials);
var   adminCommand = new CommandDocument { … }; // some admin command
var   result = adminDatabase.RunCommand(adminCommand);




                                                                               48
•  Some	
  helper	
  methods	
  require	
  admin	
  credenIals	
  
var adminCredentials = new MongoCredentials("user", "pwd", true);
var result = database.RenameCollection(
    "from", "to", adminCredentials);




                                                                     49
Open	
  source,	
  high	
  performance	
  database	
  


             Q&A
         Robert Stam
   Software Engineer, 10gen


                                                         	
  

                                                                50

More Related Content

What's hot

April 2010 - JBoss Web Services
April 2010 - JBoss Web ServicesApril 2010 - JBoss Web Services
April 2010 - JBoss Web ServicesJBug Italy
 
Building High Perf Web Apps - IE8 Firestarter
Building High Perf Web Apps - IE8 FirestarterBuilding High Perf Web Apps - IE8 Firestarter
Building High Perf Web Apps - IE8 FirestarterMithun T. Dhar
 
OpenDremel's Metaxa Architecture
OpenDremel's Metaxa ArchitectureOpenDremel's Metaxa Architecture
OpenDremel's Metaxa ArchitectureCamuel Gilyadov
 
DocBlox: your source matters @ #pfc11
DocBlox: your source matters @ #pfc11DocBlox: your source matters @ #pfc11
DocBlox: your source matters @ #pfc11Mike van Riel
 
Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Adam Tomat
 
Lucene for Solr Developers
Lucene for Solr DevelopersLucene for Solr Developers
Lucene for Solr DevelopersErik Hatcher
 
Modeling Tricks My Relational Database Never Taught Me
Modeling Tricks My Relational Database Never Taught MeModeling Tricks My Relational Database Never Taught Me
Modeling Tricks My Relational Database Never Taught MeDavid Boike
 
Php Development With Eclipde PDT
Php Development With Eclipde PDTPhp Development With Eclipde PDT
Php Development With Eclipde PDTBastian Feder
 
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013Steven Francia
 
BGOUG 2012 - XML Index Strategies
BGOUG 2012 - XML Index StrategiesBGOUG 2012 - XML Index Strategies
BGOUG 2012 - XML Index StrategiesMarco Gralike
 
Using existing language skillsets to create large-scale, cloud-based analytics
Using existing language skillsets to create large-scale, cloud-based analyticsUsing existing language skillsets to create large-scale, cloud-based analytics
Using existing language skillsets to create large-scale, cloud-based analyticsMicrosoft Tech Community
 
Solr Query Parsing
Solr Query ParsingSolr Query Parsing
Solr Query ParsingErik Hatcher
 
Rich Internet Applications con JavaFX e NetBeans
Rich Internet Applications  con JavaFX e NetBeans Rich Internet Applications  con JavaFX e NetBeans
Rich Internet Applications con JavaFX e NetBeans Fabrizio Giudici
 
Declarative Internal DSLs in Lua: A Game Changing Experience
Declarative Internal DSLs in Lua: A Game Changing ExperienceDeclarative Internal DSLs in Lua: A Game Changing Experience
Declarative Internal DSLs in Lua: A Game Changing ExperienceAlexander Gladysh
 
Hotsos 2013 - Creating Structure in Unstructured Data
Hotsos 2013 - Creating Structure in Unstructured DataHotsos 2013 - Creating Structure in Unstructured Data
Hotsos 2013 - Creating Structure in Unstructured DataMarco Gralike
 
JUnit5 and TestContainers
JUnit5 and TestContainersJUnit5 and TestContainers
JUnit5 and TestContainersSunghyouk Bae
 
Web2py tutorial to create db driven application.
Web2py tutorial to create db driven application.Web2py tutorial to create db driven application.
Web2py tutorial to create db driven application.fRui Apps
 
Kotlin @ Coupang Backed - JetBrains Day seoul 2018
Kotlin @ Coupang Backed - JetBrains Day seoul 2018Kotlin @ Coupang Backed - JetBrains Day seoul 2018
Kotlin @ Coupang Backed - JetBrains Day seoul 2018Sunghyouk Bae
 
Restful App Engine
Restful App EngineRestful App Engine
Restful App EngineRyan Morlok
 

What's hot (20)

April 2010 - JBoss Web Services
April 2010 - JBoss Web ServicesApril 2010 - JBoss Web Services
April 2010 - JBoss Web Services
 
Building High Perf Web Apps - IE8 Firestarter
Building High Perf Web Apps - IE8 FirestarterBuilding High Perf Web Apps - IE8 Firestarter
Building High Perf Web Apps - IE8 Firestarter
 
OpenDremel's Metaxa Architecture
OpenDremel's Metaxa ArchitectureOpenDremel's Metaxa Architecture
OpenDremel's Metaxa Architecture
 
No sql way_in_pg
No sql way_in_pgNo sql way_in_pg
No sql way_in_pg
 
DocBlox: your source matters @ #pfc11
DocBlox: your source matters @ #pfc11DocBlox: your source matters @ #pfc11
DocBlox: your source matters @ #pfc11
 
Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018
 
Lucene for Solr Developers
Lucene for Solr DevelopersLucene for Solr Developers
Lucene for Solr Developers
 
Modeling Tricks My Relational Database Never Taught Me
Modeling Tricks My Relational Database Never Taught MeModeling Tricks My Relational Database Never Taught Me
Modeling Tricks My Relational Database Never Taught Me
 
Php Development With Eclipde PDT
Php Development With Eclipde PDTPhp Development With Eclipde PDT
Php Development With Eclipde PDT
 
Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013Build your first MongoDB App in Ruby @ StrangeLoop 2013
Build your first MongoDB App in Ruby @ StrangeLoop 2013
 
BGOUG 2012 - XML Index Strategies
BGOUG 2012 - XML Index StrategiesBGOUG 2012 - XML Index Strategies
BGOUG 2012 - XML Index Strategies
 
Using existing language skillsets to create large-scale, cloud-based analytics
Using existing language skillsets to create large-scale, cloud-based analyticsUsing existing language skillsets to create large-scale, cloud-based analytics
Using existing language skillsets to create large-scale, cloud-based analytics
 
Solr Query Parsing
Solr Query ParsingSolr Query Parsing
Solr Query Parsing
 
Rich Internet Applications con JavaFX e NetBeans
Rich Internet Applications  con JavaFX e NetBeans Rich Internet Applications  con JavaFX e NetBeans
Rich Internet Applications con JavaFX e NetBeans
 
Declarative Internal DSLs in Lua: A Game Changing Experience
Declarative Internal DSLs in Lua: A Game Changing ExperienceDeclarative Internal DSLs in Lua: A Game Changing Experience
Declarative Internal DSLs in Lua: A Game Changing Experience
 
Hotsos 2013 - Creating Structure in Unstructured Data
Hotsos 2013 - Creating Structure in Unstructured DataHotsos 2013 - Creating Structure in Unstructured Data
Hotsos 2013 - Creating Structure in Unstructured Data
 
JUnit5 and TestContainers
JUnit5 and TestContainersJUnit5 and TestContainers
JUnit5 and TestContainers
 
Web2py tutorial to create db driven application.
Web2py tutorial to create db driven application.Web2py tutorial to create db driven application.
Web2py tutorial to create db driven application.
 
Kotlin @ Coupang Backed - JetBrains Day seoul 2018
Kotlin @ Coupang Backed - JetBrains Day seoul 2018Kotlin @ Coupang Backed - JetBrains Day seoul 2018
Kotlin @ Coupang Backed - JetBrains Day seoul 2018
 
Restful App Engine
Restful App EngineRestful App Engine
Restful App Engine
 

Similar to Webinar: What's new in the .NET Driver

Webinar: What's new in the .NET Driver
Webinar: What's new in the .NET DriverWebinar: What's new in the .NET Driver
Webinar: What's new in the .NET DriverMongoDB
 
Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsRichie Rump
 
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsFI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsPetr Dvorak
 
MongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewAntonio Pintus
 
Cappuccino - A Javascript Application Framework
Cappuccino - A Javascript Application FrameworkCappuccino - A Javascript Application Framework
Cappuccino - A Javascript Application FrameworkAndreas Korth
 
02 beginning code first
02   beginning code first02   beginning code first
02 beginning code firstMaxim Shaptala
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOCCarl Lu
 
C++ training
C++ training C++ training
C++ training PL Sharma
 
INT 222.pptx
INT 222.pptxINT 222.pptx
INT 222.pptxSaunya2
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its TypesMuhammad Hammad Waseem
 
Examiness hints and tips from the trenches
Examiness hints and tips from the trenchesExaminess hints and tips from the trenches
Examiness hints and tips from the trenchesIsmail Mayat
 
MFF UK - Introduction to iOS
MFF UK - Introduction to iOSMFF UK - Introduction to iOS
MFF UK - Introduction to iOSPetr Dvorak
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application DevelopmentDhaval Kaneria
 
Csharp introduction
Csharp introductionCsharp introduction
Csharp introductionSireesh K
 
Untangling - fall2017 - week 9
Untangling - fall2017 - week 9Untangling - fall2017 - week 9
Untangling - fall2017 - week 9Derek Jacoby
 

Similar to Webinar: What's new in the .NET Driver (20)

Webinar: What's new in the .NET Driver
Webinar: What's new in the .NET DriverWebinar: What's new in the .NET Driver
Webinar: What's new in the .NET Driver
 
Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic Unicorns
 
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsFI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS Basics
 
MongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overview
 
Cappuccino - A Javascript Application Framework
Cappuccino - A Javascript Application FrameworkCappuccino - A Javascript Application Framework
Cappuccino - A Javascript Application Framework
 
React-Native Lecture 11: In App Storage
React-Native Lecture 11: In App StorageReact-Native Lecture 11: In App Storage
React-Native Lecture 11: In App Storage
 
02 beginning code first
02   beginning code first02   beginning code first
02 beginning code first
 
02 beginning code first
02   beginning code first02   beginning code first
02 beginning code first
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOC
 
C++ training
C++ training C++ training
C++ training
 
INT 222.pptx
INT 222.pptxINT 222.pptx
INT 222.pptx
 
Real World MVC
Real World MVCReal World MVC
Real World MVC
 
Intro to .NET and Core C#
Intro to .NET and Core C#Intro to .NET and Core C#
Intro to .NET and Core C#
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
Examiness hints and tips from the trenches
Examiness hints and tips from the trenchesExaminess hints and tips from the trenches
Examiness hints and tips from the trenches
 
MFF UK - Introduction to iOS
MFF UK - Introduction to iOSMFF UK - Introduction to iOS
MFF UK - Introduction to iOS
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Csharp introduction
Csharp introductionCsharp introduction
Csharp introduction
 
Constructor
ConstructorConstructor
Constructor
 
Untangling - fall2017 - week 9
Untangling - fall2017 - week 9Untangling - fall2017 - week 9
Untangling - fall2017 - week 9
 

More from MongoDB

MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 MongoDB SoCal 2020: MongoDB Atlas Jump Start MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB SoCal 2020: MongoDB Atlas Jump StartMongoDB
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB
 

More from MongoDB (20)

MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 MongoDB SoCal 2020: MongoDB Atlas Jump Start MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
 

Recently uploaded

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

Webinar: What's new in the .NET Driver

  • 1. Open  source,  high  performance  database   What’s New in the C#/.NET Driver Robert Stam Software Engineer, 10gen July  19,  2012   1
  • 2. •  The  C#/.NET  driver   –  Is  wriFen  in  C#   –  Can  be  called  from  other  .NET  languages   •  Where  to  find  more  informaIon:   –  hFp://www.mongodb.org/display/DOCS/CSharp+Language+Center     2
  • 3. •  Recent  versions   –  1.4      Introduced  support  for  LINQ  queries   –  1.4.1    Added  support  for  addiIonal  LINQ  queries   –  1.4.2    A  few  bug  fixes   –  1.5      SerializaIon  changes,  new  Query  builders   •  Upcoming  version   –  1.6      Support  for  new  server  2.2  features   3
  • 4. •  Today’s  topics:   –  SerializaIon  (POCOs)   –  Handling  schema  evoluIon   –  The  new  Query  builders   –  LINQ  queries   –  AuthenIcaIon   4
  • 5. •  Documents  are  stored  as  BSON  in  the  database   •  When  you  read  a  document  from  the  database  what   do  you  get  back  in  your  C#  program?   •  You  can  choose  to  get  back  either:   –  A  BsonDocument   –  A  plain  old  C#  object  (POCO)  that  you  defined   •  SerializaIon  is  the  process  by  which  a  POCO  instance   is  transformed  to  a  BSON  document  and  back   5
  • 6. •  An  in  memory  representaIon  of  a  BSON  document   •  It’s  not  the  original  binary  BSON,  it’s  the  result  of   decoding  the  binary  BSON   •  Very  similar  to  a  DicIonary<string,  BsonValue>   •  Important  classes:   –  BsonValue  (abstract  base  class)   –  BsonInt32,  BsonInt64,  BsonString,  BsonDateTime,  …   –  BsonDocument,  BsonArray   •  More  informaIon:   hFp://www.mongodb.org/display/DOCS/CSharp+Driver +Tutorial#CSharpDriverTutorial-­‐BsonValueandsubclasses   6
  • 7. •  Very  likely  you  will  want  to  use  your  own  domain   classes  instead  of  BsonDocument   •  SerializaIon  makes  that  possible   •  Sample  code:   var collection = database.GetCollection<Employee>("employees"); var employee = collection.FindOne( Query<Employee>.EQ(e => e.EmployeeNumber, 1234)); employee.Salary += 1000; collection.Save(employee); 7
  • 8. •  The  driver  users  serializers  to  convert  POCOs  to  and   from  BSON  documents   •  A  serializer  is  a  class  that  implements  IBsonSerializer   •  A  serializer  is  registered  by  calling   BsonSerializer.RegisterSerializer   •  The  driver  provides  many  serializers  for   common  .NET  types   •  You  can  write  your  own  if  you  need  to   •  Class  map  based  serializaIon  works  automaIcally  for   your  POCOs  if  you  follow  a  few  simple  rules   8
  • 9. •  If  your  POCOs  follow  these  rules  they  can  be   serialized  automaIcally  by  the  C#/.NET  driver:   –  Has  a  public  no-­‐argument  constructor   –  Has  a  public  get/set  property  for  each  value  that  you  want   to  have  serialized   •  In  most  cases  automaIc  class  map  based   serializaIon  requires  you  to  do:  NOTHING!   •  You  only  need  to  do  something  when  you  want  to   override  the  automaIc  serializaIon   9
  • 10. •  One  way  to  configure  automaIc  serializaIon  is  by   annotaIng  your  class  with  aFributes   [BsonIgnoreExtraElements] public class Employee { [BsonId] public ObjectId EmployeeNumber { get; set; } [BsonElement("nm")] public string Name { get; set; } [BsonDateTimeOptions(DateOnly = true)] public DateTime DateOfBirth { get; set; } [BsonRepresentation(BsonType.Int64)] public int Salary { get; set; } } 10
  • 11. •  If  you  want  your  domain  classes  to  be  independent  of   your  persistence  layer  you  can  configure  serializaIon   in  code  instead  of  using  aFributes   BsonClassMap.RegisterClassMap<Employee>(cm => { cm.AutoMap(); cm.SetIgnoreExtraElements(true); cm.SetIdMember(cm.GetMemberMap(c => c.EmployeeNumber)); cm.GetMemberMap(c => c.Name).SetElementName("nm"); cm.GetMemberMap(c => c.DateOfBirth) .SetSerializationOptions( DateTimeSerializationOptions.DateOnlyInstance); cm.GetMemberMap(c => c.Salary) .SetRepresentation(BsonType.Int64); }); 11
  • 12. •  Common  changes  to  your  schema   –  You  added  a  new  property   –  You  removed  an  exisIng  property   –  You  renamed  an  exisIng  property   –  You  changed  the  representaIon  of  an  exisIng  property   –  You  changed  the  type  of  an  exisIng  property   •  MigraIon  strategies   –  All  at  once  using  an  upgrade  script  (much  easier)   –  Incremental   12
  • 13. •  ExisIng  documents  don't  have  a  value  for  the  new   property,  new  documents  automaIcally  will   •  When  you  deserialize  a  document  that  doesn't  have   that  element,  the  property  in  your  class  will  be  null   (or  zero)   •  You  can  provide  a  default  value  for  missing  elements   if  you  want   [BsonDefaultValue(Status.Active)] public Status EmployeeStatus { get; set; } // or cm.GetMemberMap(c => c.EmployeeStatus) .SetDefaultValue(Status.Active); 13
  • 14. •  ExisIng  documents  sIll  have  an  element  for  the   property  that  you  removed   •  An  excepIon  will  be  thrown  when  a  document   containing  the  removed  property  is  deserialized   because  we  don't  know  what  to  do  with  the   unexpected  element   •  You  could  handle  this  in  two  ways   –  Ignore  the  extra  element   –  Add  an  ExtraElements  property  to  your  class   14
  • 15. •  If  you  just  want  to  rename  the  property  in  your  code   but  keep  the  same  element  name  in  the  database   use  the  [BsonElement]  aFribute  or  the   SetElementName  method   •  If  you  use  a  migraIon  script  to  update  the  enIre   collecIon  at  once  simply  change  the  name  of  the   element  in  the  database   •  Otherwise,  consider  wriIng  a  custom  serializer  or   perhaps  just  implemenIng  ISupportIniIalize   15
  • 16. public class C : ISupportInitialize { [BsonExtraElements] public BsonDocument ExtraElements; public void BeginInit() { } public void EndInit() { // check ExtraElements for elements with old names // and load them into the appropriate property // also clear them out of ExtraElements so they won't // be saved back to the document again } } 16
  • 17. •  If  the  C#  type  didn't  change  and  the  two   representaIons  are  compaIble  you  don't  need  to  do   anything.  Old  documents  can  sIll  be  read  and  new   documents  will  be  wriFen  with  the  new   representaIon   •  Otherwise,  you  will  either  have  to  write  a  custom   serializer  or  a  migraIon  script   17
  • 18. •  If  the  new  type  is  compaIble  with  the  old  type  (e.g.   you  changed  from  Int32  to  Int64)  you  don't  need  to   do  anything   •  Otherwise,  you  will  have  to  write  a  migraIon  script   or  a  custom  serializer   18
  • 19. •  What  is  a  query?   –  Anything  that  implements  IMongoQuery   –  IMongoQuery  has  an  implied  contract:  that  when  the   object  is  serialized  to  a  BSON  document  it  will  be  a  valid   MongoDB  query   •  Ways  to  create  query:   –  new  QueryDocument  {  …  }   –  new  QueryWrapper(object  query)   –  Use  the  untyped  query  builder   –  Use  the  typed  query  builder   19
  • 20. •  Version  1.5  introduced  a  new  query  builder   •  What  was  wrong  with  the  old  query  builder?   –  It  exposed  too  many  of  the  idiosyncrasies  of  the  naIve   MongoDB  query  language  (implied  and,  unusual   restricIons  of  not,  etc…)   –  It  required  some  helper  classes  (QueryComplete,   QueryNot,  etc…)  that  were  intended  to  be  internal  but  yet   had  to  be  public  or  you  couldn't  write  a  query   –  The  query  language  rules  that  the  helper  classes  were   aFempIng  to  enforce  were  changing  from  version  to   version  of  the  server  but  the  driver  couldn't  adapt   gracefully  because  the  rules  were  encoded  in  source  code   20
  • 21. •  Works  at  a  slightly  higher  level  (e.g.,  requires   Query.And  instead  of  implied  and)   •  Much  simpler  API  (and  no  helper  classes  that  leak   out  into  the  public  API)   •  Mostly  compaIble  with  the  old  query  builder   (specially  for  simple  queries)   •  Easier  to  maintain  from  version  to  version  of  the   server   21
  • 22. •  Yes!  The  old  query  builder  will  be  removed  in  a   future  release   •  For  the  Ime  being,  you  can  conInue  using  the  old   query  builder  without  changing  your  source  code  by   adding  the  following  lines  to  the  top  of  your  source   file:   #pragma warning disable 618 // DeprecatedQuery is marked Obsolete using Query = MongoDB.Driver.Builder.DeprecatedQuery; 22
  • 23. •  There  is  a  method  in  the  untyped  query  builder  for   every  query  operator  in  the  MongoDB  query   language   Query.EQ("_id", 1234) Query.EQ("nm", "John Doe") Query.NE("EmployeeStatus", 1) // assumes Status.Active == 1 Query.GT("Salary", 100000) // are equivalent to new QueryDocument("_id", 1234) new QueryDocument("nm", "John Doe") new QueryDocument("EmployeeStatus", new BsonDocument("$ne", 1)) new QueryDocument("Salary", new BsonDocument("$gt", 100000)) 23
  • 24. •  You  have  to  know  the  element  name   –  What  if  you  use  [BsonElement]  to  change  the  element   name?   –  What  if  you  mistype  the  element  name?   •  You  have  to  correctly  serialize  any  values  yourself   –  What  if  you  serialize  the  value  wrong?   –  What  if  you  don't  even  know  how  to  serialize  the  value?   24
  • 25. •  The  typed  builder  has  the  same  methods  as  the   untyped  builder  but  is  type  aware  and  type  safe   Query<Employee>.EQ(d => d.EmployeeNumber, 1234) Query<Employee>.EQ(d => d.Name, "John Doe") Query<Employee>.NE(d => d.EmployeeStatus, Status.Active) Query<Employee>.GT(d => d.Salary, 100000) // also equivalent to new QueryDocument("_id", 1234) new QueryDocument("nm", "John Doe") new QueryDocument("EmployeeStatus", new BsonDocument("$ne", 1)) new QueryDocument("Salary", new BsonDocument("$gt", 100000)) 25
  • 26. •  The  typed  query  builder  also  lets  you  write  the   predicate  in  C#  and  it  will  be  translated  to  an   equivalent  MongoDB  query   Query<Employee>.Where(d => d.EmployeeNumber == 1234) Query<Employee>.Where(d => d.Name == "John Doe") Query<Employee>.Where(d => d.EmployeeStatus != Status.Active) Query<Employee>.Where(d => d.Salary > 100000) // still equivalent to new QueryDocument("_id", 1234) new QueryDocument("nm", "John Doe") new QueryDocument("EmployeeStatus", new BsonDocument("$ne", 1)) new QueryDocument("Salary", new BsonDocument("$gt", 100000)) 26
  • 27. var query = Query.GTE("x", 1).LTE(3); // becomes var query = Query.And(Query.GTE("x", 1), Query.LTE("x", 3)); var query = Query.Not("x").GT(1); // becomes var query = Query.Not(Query.GT("x", 1)); var query = Query.Exists("x", true); // becomes var query = Query.Exists("x"); var query = Query.Exists("x", false); // becomes var query = Query.NotExists("x"); 27
  • 28. •  LINQ  is  supported  for  queries  only  (although   Query<T>.Where  supports  wriIng  predicates  in  C#   that  can  be  used  with  Update)   •  You  opt-­‐in  to  LINQ  using  AsQueryable()   var collection = database.GetCollection<Employee>("employees"); var query = from e in collection.AsQueryable() where e.Name == "John Doe" && e.Salary >= 100000 select e; foreach (var employee in query) { // process employee } 28
  • 29. •  C#  compiler  creates  an  Expression  tree   •  C#/.NET  driver  translates  the  Expression  tree  to  an   equivalent  MongoDB  query  at  run  Ime   •  Requirements   –  For  each  C#  property  referenced  in  the  LINQ  query  the   driver  has  to  be  able  to  figure  out  the  matching  element   name  in  the  BSON  document  (using  doFed  names  for   nested  elements)   –  For  each  test  using  those  C#  properIes  the  driver  has  to   be  be  able  to  translate  the  test  into  an  equivalent   MongoDB  query  operator   29
  • 30. •  The  primary  goal  is:  we  will  only  support  LINQ   queries  that  have  a  reasonable  translaIon  to  an   equivalent  MongoDB  query   •  The  reason:  we  want  to  ensure  predictable   performance  from  LINQ  queries  (no  black  magic,  no   surprises)   •  So,  you  will  not  find:   –  Any  hidden  map/reduce  or  Javascript  tricks   –  Any  hidden  client  side  processing   •  Online  LINQ  tutorial  at:   hFp://www.mongodb.org/display/DOCS/CSharp+Driver+LINQ+Tutorial   30
  • 31. var query = from e in collection.AsQueryable<Employee>() where e.EmployeeStatus == Status.Active select e; // translates to (assuming enum value for Active is 1): { EmployeeStatus : 1 } var query = from e in collection.AsQueryable<Employee>() where e.EmployeeStatus != Status.Active select e; // translates to: { EmployeeStatus : { $ne : 1 } } 31
  • 32. var query = from e in collection.AsQueryable<Employee>() where e.EmployeeStatus == Status.Active && e.Salary > 100000 select e; // translates to: { EmployeeStatus : 1, Salary : { $gt : 100000 } } 32
  • 33. var query = from e in collection.AsQueryable<Employee>() where e.EmployeeStatus == Status.Active || e.Salary > 100000 select e; // translates to: { $or : [ { EmployeeStatus : 1 }, { Salary : { $gt : 100000 } } ]} 33
  • 34. var query = from e in collection.AsQueryable<Employee>() where e.Name.Contains("oh") select e; // translates to: { nm: /oh/s } var query = from e in collection.AsQueryable<Employee>() where e.Name.StartsWith("John") select e; // translates to: { nm: /^John/s } 34
  • 35. var query = from e in collection.AsQueryable<Employee>() where e.Name.Length == 4 select e; // translates to: { nm: /^.{4}$/s } var query = from e in collection.AsQueryable<Employee>() where string.IsNullOrEmpty(e.Name) select e; // translates to: { $or : [ { nm: { $type : 10 } }, { nm: "" } ] } 35
  • 36. var query = from e in collection.AsQueryable<Employee>() where e.Name.ToLower() == "john macadam" select e; // translates to: { nm: /^john macadam$/is } 36
  • 37. var query = from e in collection.AsQueryable<Employee>() where e.Skills[0] == "Java" select e; // translates to: { "Skills.0" : "Java" } var query = from e in collection.AsQueryable<Employee>() where e.Skills.Length == 3 select e; // translates to: { Skills : { $size : 3 } } 37
  • 38. var query = from e in collection.AsQueryable<Employee>() where e.Address.City == "Hoboken" select e; // translates to: { "Address.City" : "Hoboken" } 38
  • 39. var states = new [] { "NJ", "NY", "PA" }; var query = from e in collection.AsQueryable<Employee>() where states.Contains(e.Address.State) select e; // translates to: { "Address.State" : { $in : [ "NJ", "NY", "PA" ] } } // alternative syntax using C#/.NET driver "In" method var query = from e in collection.AsQueryable<Employee>() where e.Address.State.In(states) select e; 39
  • 40. var desiredSkills = new [] { "Java", "C#" }; var query = from e in collection.AsQueryable<Employee>() where e.Skills.ContainsAny(desiredSkills) select e; // translates to: { "Skills" : { $in : [ "Java", "C#" ] } } var query = from e in collection.AsQueryable<Employee>() where e.Skills.ContainsAll(desiredSkills) select e; // translates to: { "Skills" : { $all : [ "Java", "C#" ] } } // note: ContainsAny and ContainsAll are defined by the C#/.NET driver and are not part of standard LINQ 40
  • 41. var query = from e in collection.AsQueryable<Employee>() where e.Addresses.Any(a => a.City == "Hoboken" && a.State == "NJ") select e; // translates to: { "Addresses" : { $elemMatch : { City : "Hoboken", State : "NJ" } } } 41
  • 42. •  You  can  "Inject"  naIve  MongoDB  queries  into  a  LINQ   query  if  you  need  to  include  a  test  that  LINQ  doesn't   support,  without  giving  up  LINQ  enIrely   var query = from e in collection.AsQueryable() where e.Salary > 50000 && Query.NotExists("EmployeeStatus").Inject() select e; // translates to: { Salary : { $gt : 50000 }, EmployeeStatus : { $exists : false } } 42
  • 43. •  Turn  on  authenIcaIon  using  mongod  -­‐-­‐auth   •  AuthenIcaIon  is  at  the  database  level   •  Add  a  username/password  to  each  database  that   you  need  to  access   •  AuthenIcaIng  against  a  username/password  in  the   admin  database  is  like  root  access,  it  gives  you  access   to  all  databases   43
  • 44. •  If  you  are  using  the  same  credenIals  with  all   databases  you  can  simply  set  the  default  credenIals     // on the connection string var connectionString = "mongodb://user:pwd@localhost/?safe=true"; var server = MongoServer.Create(connectionString); // in code var settings = new MongoServerSettings() { DefaultCredentials = new MongoCredentials("user", "pwd"), SafeMode = SafeMode.True }; var server = MongoServer.Create(settings); 44
  • 45. •  If  you  are  using  different  credenIals  for  each   database  you  can  use  a  credenIals  store  to  hold  all   the  credenIals  (in  code  only,  not  supported  on  the   connecIon  string)   var credentialsStore = new MongoCredentialsStore(); credentialsStore.AddCredentials( "hr", new MongoCredentials("user1", "pwd1")); credentialsStore.AddCredentials( "inventory", new MongoCredentials("user2", "pwd2")); var settings = new MongoServerSettings { CredentialsStore = credentialsStore, SafeMode = SafeMode.True }; var server = MongoServer.Create(settings); 45
  • 46. •  You  can  also  postpone  providing  the  credenIals  unIl   you  call  GetDatabase   var credentials = new MongoCredentials("user", "pwd"); var database = server.GetDatabase("hr", credentials); 46
  • 47. •  To  authenIcate  using  the  admin  database  you  have  to   flag  the  credenIals  as  being  admin  credenIals   •  You  can  either  add  "(admin)"  to  the  end  of  the  user   name  or  set  the  Admin  flag  to  true   var cs = "mongodb://user(admin):pwd@localhost/?safe=true"; var server = MongoServer.Create(cs); var adminCredentials = new MongoCredentials("user", "pwd", true); var settings = new MongoServerSettings { DefaultCredentials = adminCredentials, SafeMode = SafeMode.True }; var server = MongoServer.Create(settings); 47
  • 48. •  You  have  to  provide  admin  credenIals  to  run  a   command  against  the  admin  database   var adminCredentials = new MongoCredentials("user", "pwd", true); var adminDatabase = server.GetDatabase("admin", adminCredentials); var adminCommand = new CommandDocument { … }; // some admin command var result = adminDatabase.RunCommand(adminCommand); 48
  • 49. •  Some  helper  methods  require  admin  credenIals   var adminCredentials = new MongoCredentials("user", "pwd", true); var result = database.RenameCollection( "from", "to", adminCredentials); 49
  • 50. Open  source,  high  performance  database   Q&A Robert Stam Software Engineer, 10gen   50