Mongo DB Atlas Apply for a mongo DB
Mongo. DB Atlas
• Apply for a mongo. DB Atlas account: • https: //www. mongodb. com/cloud/atlas • Use Mongdo. DB Atlas to create database and collection • Download mongo. DB Compass Community: • https: //www. mongodb. com/download-center/compass • Use MOngo. DB Compass to connect to the Mongo. DB Atlas from user’s computer.
Working with Mongo. DB Atlas • Apply for a mongo. DB Atlas account: • https: //www. mongodb. com/cloud/atlas • After login: • Clusters: storage for databases • Cluster 0 • Security: • To add a new database user: Click Database Access, and then click ADD NEW USER
To add a new user: Under Security, Click Database Access, and then click ADD NEW USER
Enter user name and password
Storage: Clusters
Mongo. DB Clusters and Databases • A Mongo. DB cluster may contain many databases. • A Mongo. DB database may contain many collections. • A Mongo. DB collection may contain many documents. • A Mongo. DB document is a JSON document.
Create a new database with a new collection • Click Clusters, and click Collections under Cluster 0
Click Create Database: Create a new database: mydb with a new collection: employee
Click: Insert Document
Use Tab to move to the value, and Enter to move to the next field. Choose the data type from the dropdown list. Enter 3 documents: e 1, peter, m, 65000; e 2, paul, m, 75000; e 3, mary, f, 70000.
Download Mongo. DB Compass https: //www. mongodb. com/download-center/compass
Connect using Mongo. DB Compass From Clusters/Cluster 0, Click the CONNECT button
Click: Connect with Mongo. DB Compass
Click the “I have Compass” if you already install it. And then click the Copy button to copy the connection string
Connect to Mongo. DB Atlas from Compass Click: Connect, then paste the connection string to the box (use Control/V to paste the connection string, and enter the database username and password (not the Mongo. DB Atlas username and pwd).
To add a new document to employee collection: Click employee collection; then click ADD DATA and Insert Document
Mongo. DB Compass Query Filter • https: //docs. mongodb. com/compass/master/query/filter/ • https: //docs. mongodb. com/manual/tutorial/query-documents/ • Query nested document: • https: //docs. mongodb. com/manual/tutorial/query-embedded-documents/ • Query array: • https: //docs. mongodb. com/manual/tutorial/query-arrays/
Query Operators for comparison https: //docs. mongodb. com/manual/reference/operator/query/ Name Description $eq Matches values that are equal to a specified value. $gt Matches values that are greater than a specified value. $gte Matches values that are greater than or equal to a specified value. $in Matches any of the values specified in an array. $lt Matches values that are less than a specified value. $lte Matches values that are less than or equal to a specified value. $ne Matches all values that are not equal to a specified value. $nin Matches none of the values specified in an array. • Example: $in • { RATING: { $in: ["A", "C" ] } }
Query filter examples Note: Field name and data are case-sensitive. • {sex: "m"}, or {sex: {$eq: "m"}} • {salary: {$gt: 50000}} • {sex: "m", salary: {$gt: 50000}} • Objects that have no eid field value: • {eid: null}
Click Options to add projection or Sort
Project Fields to Return from Query https: //docs. mongodb. com/manual/tutorial/project-fields-from-query-results/ • Suppress sex, salary, and _id fields: {sex: 0, salary: 0, _id: 0} • Equivalent to project eid, ename • Project eid, ename: {eid: 1, ename: 1}
Sorting • Increasing order 1: {sex: 1} • Decreasing order -1: {sex: -1} • {sex: 1, ename: 1}
Logical Operators for Complex Conditions Name Description $and Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. $not Inverts the effect of a query expression and returns documents that do not match the query expression. $nor Joins query clauses with a logical NOR returns all documents that fail to match both clauses. $or Joins query clauses with a logical OR returns all documents that match the conditions of either clause. • Example: {$or: [ { CITY: "SF" }, {RATING: "A" }]}
Example: • AND: • {$and: [ { CITY: "SF" }, {RATING: "A" }]} • Mongo. DB provides an implicit AND operation when specifying a comma separated list of expressions. • { CITY: “SF" }, {RATING: "A" } • OR: • { $or: [ { CITY: "SF" }, { RATING: "A"}] } • Find customers living in SF and have A or C rating • {$and: [{CITY: "SF"}, {$or: [{RATING: "A"}, {RATING: "C"}]}]} • Find customers who have C rating or live in SF with A rating: • {$or: [{RATING: "C"}, {$and: [{RATING: "A"}, {CITY: "SF"}]}]}
Using JSON to implement 1: M relationship and multivalued attribute employee: empid empname city salary phone: [phone. Number] dependents: [{depid, dept. Name, relationship}] • Employee object: { "employee": {"empid": "e 1", "emp. Name": "John", "city": "SF", "salary": 65000, "phones": ["1111", "1112", "1113"], "dependents": [{"depid": "d 1", "dname": " mary", "relationship": "spouse"}, {"depid": "d 2", "dname": "paul", "relationship": "son"}]} }
Data from slide #39 and 41 of JSON PP {"employee": {"empid": "e 2", "emp. Name": "paul", "city": "SF", "salary": 75000, "phone": ["2221", "2222"], "dependents": {"depid": "d 3", "dname": "nancy", "relationship": "spouse"}}} {"employee": {"empid": "e 3", "emp. Name": "mary", "city": "LA", "salary": 85000, "phone": "3333", "dependents": [{"depid": "d 4", "dname": "linda", "relationship": "spouse"}, {"depid": "d 5", "dname": "mia", " relationship": "daughter"}]}}
Click mydb and click CREATE COLLECTION to add a new collection: employee. Dependent. Click employee. Dependent to add data.
Copy/Paste (using Ctrl/V)the documents to insert to the database
Query Nested document: https: //docs. mongodb. com/manual/tutorial/query-array-of-documents/ “employee” is an object. We use the dot notation to access properties: Find employee e 1 document: {'employee. empid': 'e 1'}
Find employees in SF: {'employee. city': 'SF'}
Query to show employee name and dependents: {'employee. emp. Name': 1, 'employee. dependents': 1}
Sorted by emp. Name: {'employee. emp. Name': 1}
Element Operators Name Description $exists Matches documents that have the specified field. $type Selects documents if a field is of the specified type. • Documents do not have eid field: {eid: {$exists: false}} • Documents that do not have the RATING field: • { RATING: { $exists: false } } • Find documents that have the RATING field with value A or C: • { RATING: { $exists: true , $in: ["A", "C"] }}
Copy this document and insert into the mydb employee collection: { "employee": {"empid": "e 1", "emp. Name": "John", "city": "SF", "salary": 65000, "phones": ["1111", "1112", "1113"], "d ependents": [{"depid": "d 1", "dname": "mary", "relationship": "spo use"}, {"depid": "d 2", "dname": "paul", "relationship": "son"}]} }
Find employees that have dependents: {'employee. dependents': {$exists: true}}
Show name and dependents:
Aggregation Pipeline Builder https: //docs. mongodb. com/compass/master/aggregation-pipelinebuilder/
CRUD Operations • Four basic operations of database application: – Create: add new records – Read: retrieve records – Update: modify records – Delete: delete records • Database servers typically offer API to do CRUD. • Demo: using C# and Mongo. DB Atlas
Add a new document using Mongo. DB. Driver (add. New. Customer. cs) try { const string connection. String = "mongodb+srv: //dchao: your. Pwd@cluster 0 iwf 8 i. mongodb. net/test? retry. Writes=true&w=majority"; const string hosted. Web. Connection. String = connection. String; var db. Client = new Mongo. Client(hosted. Web. Connection. String); IMongo. Database db = db. Client. Get. Database("users"); var customers = db. Get. Collection<Bson. Document>("customers"); var document = new Bson. Document { { "cid", text. Box 1. Text }, { "cname", text. Box 2. Text }, { "city", text. Box 3. Text }, {"rating", text. Box 4. Text } }; customers. Insert. One(document); } catch (Exception ex) { Message. Box. Show(ex. Message); }
Retrieve one document const string connection. String = "mongodb+srv: //dchao: your. Pwd@cluster 0 iwf 8 i. mongodb. net/test? retry. Writes=true&w=majority"; const string hosted. Web. Connection. String = connection. String; var db. Client = new Mongo. Client(hosted. Web. Connection. String); IMongo. Database db = db. Client. Get. Database("users"); var customers = db. Get. Collection<Bson. Document>("customers"); var filter = Builders<Bson. Document>. Filter. Eq("CID", text. Box 1. Text); var document = customers. Find(filter). First(); text. Box 2. Text = document. Get. Value("CNAME"). To. String(); text. Box 3. Text = document. Get. Value("CITY"). To. String(); text. Box 4. Text = document. Get. Value("RATING"). To. String();
Updating a document const string connection. String = "mongodb+srv: //dchao: your. Pwd@cluster 0 iwf 8 i. mongodb. net/test? retry. Writes=true&w=majority"; const string hosted. Web. Connection. String = connection. String; var db. Client = new Mongo. Client(hosted. Web. Connection. String); IMongo. Database db = db. Client. Get. Database("users"); var customers = db. Get. Collection<Bson. Document>("customers"); var filter = Builders<Bson. Document>. Filter. Eq("CID", text. Box 1. Text); var update = Builders<Bson. Document>. Update. Set("RATING", text. Box 2. Text); customers. Update. One(filter, update);
Delete a document https: //www. mongodb. com/blog/post/quick-startcsharp-and-mongodb--delete-operations const string connection. String = "mongodb+srv: //dchao: your. Pwd@cluster 0 iwf 8 i. mongodb. net/test? retry. Writes=true&w=majority"; const string hosted. Web. Connection. String = connection. String; var db. Client = new Mongo. Client(hosted. Web. Connection. String); IMongo. Database db = db. Client. Get. Database("users"); var customers = db. Get. Collection<Bson. Document>("customers"); var delete. Filter = Builders<Bson. Document>. Filter. Eq("CID", text. Box 1. Text); customers. Delete. One(delete. Filter);
- Slides: 46