MongoDB Short Primer PDF

Summary

This document is a short primer on MongoDB using PyMongo, intended for students. It covers the basics of the NoSQL database, its structure, and how to interact with it using Python. Topics like document structure and interacting with MongoDB through Python are highlighted.

Full Transcript

Topic 4: Mongo primer A short primer on Mongo using PyMongo Dr. Daniel Yellin THESE SLIDES ARE THE PROPERTY OF DANIEL YELLIN THEY ARE ONLY FOR USE BY STUDENTS OF THE CLASS THERE IS NO PERMISSION TO DISTRIBUTE OR POST THESE SLIDES TO OTHERS Mongo, a NoSQL database N...

Topic 4: Mongo primer A short primer on Mongo using PyMongo Dr. Daniel Yellin THESE SLIDES ARE THE PROPERTY OF DANIEL YELLIN THEY ARE ONLY FOR USE BY STUDENTS OF THE CLASS THERE IS NO PERMISSION TO DISTRIBUTE OR POST THESE SLIDES TO OTHERS Mongo, a NoSQL database NoSQL databases are a good fit for programs that handle diverse data types. Because the data is stored in a JSON-like format, and has a flexible schema, it is easy for developers to use, to integrate with and evolve their programs. It is highly scalable. Assignment #2 requires running a database in a container We illustrate using the pymongo package that provides a python driver for MongoDB. Drivers are available for other programming languages. However, it is not a prerequisite for assignment #2 – you can use any DB you want. A short primer on using Mongo is given in an appendix at the end of these slides. MongoDB MongoDB is organized around: Database Documents: a BSON document, very similar to JSON. A document is simply a set of Collection field-and value pairs. Collections: a set of documents. Similar to a table in Every relational DBs. document Unlike a relational table, each Document must have document in a collection can s have different fields. an ”_id” field Databases: A database holds one or more collections of { Example documents. "_id": 1, document "name": "AC3 Phone", "colors" : ["black", "silver"], "price" : 200 } Mongo _id field Every Mongo document must have an _id field. The value of this field is either provided by the programmer when inserting the record into a Mongo DB collection. If it is not provided by the programmer, it is automatically assigned a unique value by Mongo. If the programmer assigns the value, it can be any unique identifier, including a number. If Mongo assigns the value, it is a unique id of type ObjectId. You can convert to and from string and ObjectId representations. The _id field is always the first field in the document. See this article for an explanation of ObjectIds and how likely it is that Mongo generates 2 Ids that are the same. IDs and assignment #2 IDs in REST requests for assignment #2 (like #1) must be strings. If you use default Mongo ObjectIds for the “_id” field, then you convert between ObjectIds and REST string IDs. Benefit: Mongo manages the IDs for you. If you use your own string IDs, then you must manage the IDs. Benefit: you need not convert between ObjectIds and strings. You can also use, say integers, for IDs. Then you must manage the IDs and convert between the string representation of the ID its integer representation. If you manage your own IDs, make sure you can continue to create unique IDs even if the system crashes and then comes back up. Setting up the client to use Mongo client = The python code on the right uses the pymongo.MongoClient( pymongo package. "mongodb://mongo:27017/") It assumes mongo is running in a container and the name “mongo” is the one assigned by docker-compose.yml to the mongo service db = client[“myDB”] and that mongo is listening on the port 27017. 1. The first line creates a mongo client inv = db[“inventory”] connected to the mongo service running in a container. Warning. You cannot use “localhost” in 2. The second line creates a mongo DB place of “mongo” in the example above. called myDB. You must use the name given in the docker- 3. The third line creates a collection compose.yml, in my case “mongo”. inventory in myDB. Methods on that collection are done via the python variable inv. Note: we are hardcoding the default Mongo port 27017 into our code. Not a good practice. One should set this an an environment variable outside of the code. The inventory collection Each document in the inventory name is a string. description is collection will have the form: itself a JSON record of the form: { { ”_id”: _id, “size”: size, “name”: name, “color”: color “info”: description } } where size is an integer and color is a string. Inserting and deleting documents 1. To insert a document into the newdoc = { collection, use the “_id”: 1, insert_one method on a “name”: collection object. You give it “slacks”, the JSON for the document to be inserted. “info”: { “size”: 18, 2. To delete a document from the collection, use the delete_one “color”: ”blue”} method. You provide the value } of field (or fields) identifying the response = document to be deleted. You inv.insert_one(newdoc) can check from the response that the document was successfully deleted. resp = inv.delete_one({"_id": 8}) if resp.deletedCount==0: In this example, the programmer provides the _id field with print(“document not integer values. deleted”) Finding documents To find a the document into the doc = inv.find_one({“_id”:3}) collection, use the find_one if doc is None: method on a collection object.... 1. Find a document whose “_id” field equals 3. You can check if a doc = document was found or not by inv.find_one({“info.size”:18} ) checking if the return value is name = doc[“name”] None. size = doc[“info”][“size”] … 2. Find a document whose “info” field is a JSON document with a field “size” whose value is 18 and cursor = inv.find({}} then retrieve the value of its items = list(cursor) “name” & “size” fields. for item in items: 3. The find({}) method returns size = item[“info”][“size”] all items in the collection … Finding and updating documents (cont) 1. Find those document whose cursor = inv.find({"_id": “_id” field is great than or {"$gte": 3}}) equal to 3. 2. Update a document whose “_id” field is equal to 8, and result = update its name field to be inv.update_one({"_id": 8}, “shirt”. {"$set": {”name": “shirt”}}) Further reading https://www.mongodb.com/docs/ https://pymongo.readthedocs.io/en/stable/

Use Quizgecko on...
Browser
Browser