1. Trang chủ
  2. » Công Nghệ Thông Tin

Mongo DB Document Database Succinctly by Agus Kurniawan

106 269 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 106
Dung lượng 2,48 MB

Nội dung

MongoDB is an opensource document database, and the leading NoSQL database. Written in C++, MongoDB features: DocumentOriented Storage: JSONstyle documents with dynamic schemas offer simplicity and power. Full Index Support: Index on any attribute, just like youre used to. Replication High Availability: Mirror across LANs and WANs for scale and peace of mind. AutoSharding: Scale horizontally without compromising functionality. Querying: Rich, documentbased queries. Fast InPlace Updates: Atomic modifiers for contentionfree performance. MapReduce Flexible aggregation and data processing. GridFS: Store files of any size without complicating your stack. MongoDB Management Service »Monitoring and backup designed for MongoDB. Partner with MongoDB: Reduce cost, accelerate time to market, and mitigate risk with proactive support and enterprisegrade capabilities.

2 By Agus Kurniawan Foreword by Daniel Jebaraj 3 Copyright © 2014 by Syncfusion Inc. 2501 Aerial Center Parkway Suite 200 Morrisville, NC 27560 USA All rights reserved. mportant licensing information. Please read. This book is available for free download from www.syncfusion.com on completion of a registration form. If you obtained this book from any other source, please register and download a free copy from www.syncfusion.com. This book is licensed for reading only if obtained from www.syncfusion.com. This book is licensed strictly for personal or educational use. Redistribution in any form is prohibited. The authors and copyright holders provide absolutely no warranty for any information provided. The authors and copyright holders shall not be liable for any claim, damages, or any other liability arising from, out of, or in connection with the information in this book. Please do not use this book if the listed terms are unacceptable. Use shall constitute acceptance of the terms listed. SYNCFUSION, SUCCINCTLY, DELIVER INNOVATION WITH EASE, ESSENTIAL, and .NET ESSENTIALS are the registered trademarks of Syncfusion, Inc. Technical Reviewer: Zoran Maksimovic Copy Editor: Benjamin Ball Acquisitions Coordinator: Jessica Rightmer, senior marketing strategist, Syncfusion, Inc. Proofreader: Graham High, content producer, Syncfusion, Inc. I 4 Table of Contents The Story behind the Succinctly Series of Books 7 About The Author 9 Introduction to MongoDB 10 What is MongoDB? 10 Installation 10 C# and Development Tools 11 MongoDB Driver for C# 13 Chapter 1 Connecting to MongoDB 17 Creating a Console Application 17 Adding MongoDB Driver Files 18 Writing Code 20 Testing 21 Chapter 2 Creating and Deleting the Database 22 Creating a Database 22 Getting the List of Databases 23 Deleting a Database 24 Chapter 3 Database Collection 25 Creating a Database Collection 25 Reading a Database Collection 26 Deleting a Database Collection 26 Chapter 4 Collection Data 28 Creating Collection Data 28 Reading Collection Data 31 5 Updating Collection Data 33 Deleting Collection Data 35 Chapter 5 Finding and Querying Data 36 Finding Data 36 Query 38 Query and Remove 40 Chapter 6 Binary and Image Collection Data 42 Inserting an Image or Binary File 42 Mapping GridFS and Collection Data 43 Reading GridFS Data 44 Deleting GridFS Data 45 Chapter 7 Embedded Document 47 Preparation 47 Simple Embedded Document 47 Embedded Document Collection 52 Chapter 8 LINQ 59 Preparation 59 Querying with LINQ 61 Chapter 9 Working with MongoDB Shell 65 What is MongoDB Shell? 65 Databases 68 Database User 69 Document 71 Editing a Document 72 Deleting a Document 73 Comparison Operators 73 6 Limiting and Sorting 74 AND and OR Operators 74 Chapter 10 MongoDB and Windows Forms 76 Creating a Project 76 Adding the MongoDB Driver 77 Design Form 78 Writing Code 79 Chapter 11 MongoDB and ASP.NET 83 Creating a Project 83 Adding the MongoDB Driver 83 Design Form 84 Writing Code 85 Chapter 12 MongoDB and ASP.NET MVC 88 Creating a Project 88 Adding the MongoDB Driver 89 Adding a Model 90 Adding a View 92 Modifying the Controller 94 Chapter 13 Export and Import Database 96 Exporting Data 96 Importing Data 98 Chapter 14 Back up and Restore 100 Back up 100 Restore 103 7 The Story behind the Succinctly Series of Books Daniel Jebaraj, Vice President Syncfusion, Inc. taying on the cutting edge As many of you may know, Syncfusion is a provider of software components for the Microsoft platform. This puts us in the exciting but challenging position of always being on the cutting edge. Whenever platforms or tools are shipping out of Microsoft, which seems to be about every other week these days, we have to educate ourselves, quickly. Information is plentiful but harder to digest In reality, this translates into a lot of book orders, blog searches, and Twitter scans. While more information is becoming available on the Internet and more and more books are being published, even on topics that are relatively new, one aspect that continues to inhibit us is the inability to find concise technology overview books. We are usually faced with two options: read several 500+ page books or scour the web for relevant blog posts and other articles. Just as everyone else who has a job to do and customers to serve, we find this quite frustrating. The Succinctly series This frustration translated into a deep desire to produce a series of concise technical books that would be targeted at developers working on the Microsoft platform. We firmly believe, given the background knowledge such developers have, that most topics can be translated into books that are between 50 and 100 pages. This is exactly what we resolved to accomplish with the Succinctly series. Isn’t everything wonderful born out of a deep desire to change things for the better? The best authors, the best content Each author was carefully chosen from a pool of talented experts who shared our vision. The book you now hold in your hands, and the others available in this series, are a result of the authors’ tireless work. You will find original content that is guaranteed to get you up and running in about the time it takes to drink a few cups of coffee. S 8 Free forever Syncfusion will be working to produce books on several topics. The books will always be free. Any updates we publish will also be free. Free? What is the catch? There is no catch here. Syncfusion has a vested interest in this effort. As a component vendor, our unique claim has always been that we offer deeper and broader frameworks than anyone else on the market. Developer education greatly helps us market and sell against competing vendors who promise to “enable AJAX support with one click,” or “turn the moon to cheese!” Let us know what you think If you have any topics of interest, thoughts, or feedback, please feel free to send them to us at succinctly-series@syncfusion.com. We sincerely hope you enjoy reading this book and that it helps you better understand the topic of study. Thank you for reading. Please follow us on Twitter and “Like” us on Facebook to help us spread the word about the Succinctly series! 9 About The Author Agus Kurniawan is one of the founders of PECollege.net. He is a lecturer and author, and has been a Microsoft MVP since 2004. He has more than 10 years of software development experience, especially with Microsoft technology, and some experience related to the Linux platform. You can reach Agus via email at aguskur@hotmail.com and through his blog, http://blog.aguskurniawan.net. 10 Introduction to MongoDB What is MongoDB? To get a better understanding of MongoDB, the official website for MongoDB is an excellent resource: http://www.mongodb.org/display/DOCS/Introduction. Installation If you have a Linux platform, you must first update the repository. sudo apt-key adv keyserver keyserver.ubuntu.com recv 7F0CEB10 sudo apt-get update Then, install the MongoDB database engine. sudo apt-get install mongodb-10gen After that, install the MongoDB driver for Node.js. sudo npm install mongodb For Mac and Windows platforms, you can download MongoDB and install it directly from the project website, http://www.mongodb.org/. I recommend installing the MongoDB server engine as a Windows service. In order to install MongoDB on Windows: 1. Unzip the downloaded files in the C:\mongo folder. 2. Open the Command Prompt window with administrator privileges and navigate to the C:\mongo\bin folder. 3. Run the following command (you may need to create the c:\mongo\data\db and c:\mongo\log folders before running the command). mongod logpath "c:\mongo\log\log.log" logappend dbpath "c:\mongo\data\db" directoryperdb install After installation, you will get the MongoDB files as shown in Figure 1. If successful, you will see MongoDB in the Windows Services panel, shown in Figure 2. [...]... server = MongoServer.Create(connectionString); MongoDatabase database = server.GetDatabase(databaseName); Console.WriteLine( "Database name: {0}", database. Name); If the database name does not exist, the MongoDB driver will create it automatically It first creates a database in cache memory If data comes into this database, MongoDB will flush the database 22 Figure 17: Creating a database in MongoDB Getting... System; using MongoDB.Driver; using MongoDB.Bson; Creating Collection Data Before creating collection data, get the MongoDatabase object from the MongoServer string connectionString = "mongodb://localhost"; string databaseName = "csharp"; // Get database Console.WriteLine(">>Get database" ); MongoServer server = MongoServer.Create(connectionString); MongoDatabase database = server.GetDatabase(databaseName);... Creating a Database Collection First, get the MongoDatabase object from the MongoServer object by passing a database connection string string connectionString = "mongodb://localhost"; string databaseName = "csharp"; Console.WriteLine(">>Create/Get database" ); MongoServer server = MongoServer.Create(connectionString); MongoDatabase database = server.GetDatabase(databaseName); Console.WriteLine( "Database. .. Manager as shown in Figure 8 Add the MongoDB.Driver.dll and MongoDB.Bson.dll files from the installed MongoDB folder 15 Figure 8: Adding MongoDB driver files Click OK You will see the MongoDB driver referenced in your project Figure 9: C# project with reference to MongoDB driver 16 Chapter 1 Connecting to MongoDB In this chapter, you will learn how to connect to a MongoDB database server using C# Creating... MongoDatabase object using System; using System.Collections.Generic; using MongoDB.Driver; namespace Lab2 { class Program { static void Main(string[] args) { string connectionString = "mongodb://localhost"; string databaseName = "csharp"; MongoServer server = MongoServer.Create(connectionString); MongoDatabase database = server.GetDatabase(databaseName); // Drop database Console.WriteLine(">>Drop database. .. and database Create a new database called csharp You can create the database by using the MongoDatabase object It can be obtained from MongoServer by calling the GetDatabase() method When you call GetDatabase(), you should specify the database name as the method’s parameter string connectionString = "mongodb://localhost"; string databaseName = "csharp"; Console.WriteLine(">>Create/Get database" ); MongoServer... connectionString = "mongodb://localhost"; string databaseName = "csharp"; MongoServer server = MongoServer.Create(connectionString); Console.WriteLine(">>List of database: "); List dbs = new List(server.GetDatabaseNames()); foreach (var dbName in dbs) { Console.WriteLine(dbName); } } } } 23 Figure 18: Getting a list of databases Deleting a Database To delete a database from the MongoDB server,... a database collection using C# To do this, we create a Console Application Once created, include the following namespaces using using using using System; MongoDB.Driver; MongoDB.Bson; MongoDB.Driver.Builders; Finding Data MongoDB is a NoSQL database, so if you want to find data you can use the FindOne() and Find() methods First, prepare the database object for MongoDB string connectionString = "mongodb://localhost";... 17: Creating a database in MongoDB Getting the List of Databases You may want to know the list of databases in the current MongoDB server You can do this by calling GetDatabaseNames() from the MongoServer object The following example code gets a list of database names from the MongoDB server using System; using System.Collections.Generic; using MongoDB.Driver; namespace Lab2 { class Program { static void... Visual Studio 2010 12 Figure 4: Visual Studio 2012 MongoDB Driver for C# You can find the MongoDB driver for the NET platform at http://www.mongodb.org/display/DOCS/Drivers I use the official C# driver supported by 10gen You can download it at https://github.com/mongodb /mongo- csharp-driver/downloads If you download the MSI file, you can install MongoDB directly from the setup file You will see the setup . http://blog.aguskurniawan.net. 10 Introduction to MongoDB What is MongoDB? To get a better understanding of MongoDB, the official website for MongoDB is an excellent resource: http://www .mongodb. org/display/DOCS/Introduction the MongoDB database engine. sudo apt-get install mongodb- 10gen After that, install the MongoDB driver for Node.js. sudo npm install mongodb For Mac and Windows platforms, you can download MongoDB. Figure 8. Add the MongoDB. Driver.dll and MongoDB. Bson.dll files from the installed MongoDB folder. 16 Figure 8: Adding MongoDB driver files Click OK. You will see the MongoDB driver referenced

Ngày đăng: 12/07/2014, 17:19

TỪ KHÓA LIÊN QUAN