Created
August 9, 2015 11:45
-
-
Save harouny/a00db1065fbbcfa8d79b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async Task Main() | |
{ | |
IMongoDatabase db = new MongoClient().GetDatabase("bookStore"); | |
IMongoCollection<Author> authors = db.GetCollection<Author>("Authors"); | |
//Query single document | |
var stephenKing = await authors | |
.Find(author => author.Name == "Stephen" && author.Surname == "King") | |
.SingleAsync(); | |
stephenKing.Dump(); | |
//Query multible documents | |
var allAuthors = await authors | |
.Find(author => true) | |
.Project(author => new { | |
author.Name, | |
author.Surname | |
}) | |
.ToListAsync(); | |
allAuthors.Dump(); | |
//Query using query builder | |
var builder = Builders<Author>.Filter; | |
var filter = Builders<Author>.Filter.Eq("Books.Genre", "Fantasy"); | |
var fantacyAuthors = await authors | |
.Find(filter) | |
.ToListAsync(); | |
fantacyAuthors.Dump(); | |
} | |
// Define other methods and classes here | |
[BsonIgnoreExtraElements] | |
public class Author { | |
public string Name { get; set; } | |
public string Surname { get; set; } | |
public Book[] Books { get; set; } | |
} | |
public class Book { | |
public string Title { get; set; } | |
public string Genre { get; set; } | |
public DateTime? PublishedOn { get; set; } | |
public Double? Price { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment