简体   繁体   中英

Project a single array item mongodb C#

I have the following json

{
   "name":"Student",
    "Classes" : ["Chemistry","Math","Algebra"]
 }

and the following poco

public class Studen
{
     public string Name {get;set;}
     public string[] Classes {get; set;}
}

I want to query a specific student that takes a specific class (say Math), and the poco to have a single item in the array which is the "Math" string

if your collection is a variable collection:

collection.Find(x => x.Classes.Contains("Math"))
          .Project(s => 
                   new Student {
                        Name = s.Name,
                        Classes = s.Classes.Where(c=>c=="Math").ToArray()})
         .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM