Skip to content

Instantly share code, notes, and snippets.

@harouny
Created August 9, 2015 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harouny/a00db1065fbbcfa8d79b to your computer and use it in GitHub Desktop.
Save harouny/a00db1065fbbcfa8d79b to your computer and use it in GitHub Desktop.
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