static void Main() { using (var documentStore = new DocumentStore { Url = "http://localhost:8080" }.Initialize()) { using (var session = documentStore.OpenSession()) { for(var i = 0; i < 10; i++) { var post = new Post { BlogId = "blogs/1", AuthorId = "users/PastyGeek", Name = "PastyGeek", PostDate = DateTime.Now.AddHours(-1).AddMinutes(i), Title = "Glutonous Gloating CodeMash Post " + i, Body = "You wouldn't believe how much more fun I'm having than you!", Tags = new List<string> { "tag" + i, "codemash" } }; session.Store(post); } session.SaveChanges(); Console.WriteLine("Query for all posts."); var posts = session.Query<Post>().OrderByDescending(p => p.PostDate).ToList(); foreach(var post in posts) Console.WriteLine("Post ID: {0}, Title: {1}", post.Id, post.Title); Console.WriteLine("Query posts by tag."); var postsByTag = session.Query<Post>() .Where(p => p.Tags.Any(t => t == "tag1")); foreach (var post in postsByTag) Console.WriteLine("Post ID: {0}, Title: {1}, Tags: {2}", post.Id, post.Title, string.Join(", ", post.Tags.ToArray())); Console.ReadKey(true); } } }
static void Main() { var shards = new Shards { new DocumentStore {Url = "http://localhost:8080", Identifier = "Posts"}, new DocumentStore { Url = "http://localhost:8081", Identifier = "Users", Conventions = {DocumentKeyGenerator = user => "users/" + ((User) user).Name} } }; var shardStrategy = new ShardStrategy { ShardAccessStrategy = new ParallelShardAccessStrategy(), ShardSelectionStrategy = new BlogShardSelectionStrategy(), ShardResolutionStrategy = new BlogShardResolutionStrategy() }; using (var documentStore = new ShardedDocumentStore(shardStrategy, shards).Initialize()) { using (var session = documentStore.OpenSession()) { var user = new User { Name = "PastyGeek" }; session.Store(user); session.SaveChanges(); var post = new Post { AuthorId = user.Id, Name = user.Name, BlogId = "blogs/1", Title = "More CodeMash Gloating!", Body = "You wouldn't believe how much more fun I'm having than you!", PostDate = DateTime.Now, Tags = new List<string> { "codemash", "gloating" } }; session.Store(post); session.SaveChanges(); } } }