c# - .Net MongoDB Driver Subcollection, Builders, and Projections -


(forgive newbiness.) if given following classes how

  1. query on property of subcollection. (avoiding strings as possible)
  2. project , map result given dto?

classes:

public class customer{      public objectid id;     public string firstname;     public string lastname;     .     .        . (other properties specific customer profile)     public ienumerable<transaction> transactions; }  public class transaction{     public objectid itemid;     public datetime purchasedate;     .     .     . (etc.) }  public class transactiondto{     public objectid customerid;     public string firstname;     public objectid itemid;     public datetime purchasedate;  } 

aside compile issues there might be, wrong/right approach mongo?

how list of transactions itemid == x projected transactiondto using builders class provided in mongo driver.

this i'm headed i've run road blocks.

to query main collection (customer) on specific property can build query this:

builders<customer>.filter.eq(c=>c.firstname,"bob"); 

but using syntax can't query on subcollection.

builders<customer>.filter.eq(c=>c.transactions....????, "match condition"); 

so proper way able query? projection this:

builders<customer>.projection.expression(c=> new transactiondto(){     customerid = x.id,     firstname = x.firstname, // <- these should fine.     itemid = x.transaction...???     purchasedate = x.transaction....??? }) 

mongo c# docs

i think looking use of aggregate. using collection should able similar following

assuming have following classes , want map mainwithsub

public class mainclass {     public objectid id { get; set; }     public string propone { get; set; }     public string proptwo { get; set; }     public list<subclass> subclasses { get; set; } } public class subclass {     public string propthree { get; set; } }  public class mainwithsub {     public objectid id { get; set; }     public string propone { get; set; }     public string proptwo { get; set; }     public string propthree { get; set; } } 

you can make following call collection

collection.aggregate()             .match(t => t.id == new objectid())             .unwind<mainclass, mainwithsub>(t => t.subclasses)             .match(t => t.propthree == "filter"); 

this return list of mainwithsub ( flattened version) classes filtered in on propthree


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -