the distinct method
play

The distinct() method IN TRODUCTION TO MON GODB IN P YTH ON - PowerPoint PPT Presentation

The distinct() method IN TRODUCTION TO MON GODB IN P YTH ON Donny Winston Instructor An exceptional laureate db.laureates.find_one({"prizes.2": {"$exists": True}}) {'_id': ObjectId('5baacf97f35b632bbe12c1ad'), 'born':


  1. The distinct() method IN TRODUCTION TO MON GODB IN P YTH ON Donny Winston Instructor

  2. An exceptional laureate db.laureates.find_one({"prizes.2": {"$exists": True}}) {'_id': ObjectId('5baacf97f35b632bbe12c1ad'), 'born': '0000-00-00', 'died': '0000-00-00', 'firstname': ('Comité international de la Croix Rouge' '(International Committee of the Red Cross)'), 'gender': 'org', 'id': '482', 'prizes': [{'affiliations': [[]], 'category': 'peace', 'share': '1', 'year': '1917'}, {'affiliations': [[]], 'category': 'peace', 'share': '1', 'year': '1944'}, {'affiliations': [[]], 'category': 'peace', 'share': '2', 'year': '1963'}]} INTRODUCTION TO MONGODB IN PYTHON

  3. Using .distinct() db.laureates.distinct("gender") ['male', 'female', 'org'] A convenience method for a common aggregation (like count_documents ) We will learn how to create custom aggregations in Chapter 4 distinct aggregation is ef�cient if there is a collection index on the �eld We will learn how to create an index in Chapter 3 No index needed here: collection �ts in memory, has ?1,000 documents INTRODUCTION TO MONGODB IN PYTHON

  4. .distinct() with dot notation db.laureates.find_one({"prizes.2": {"$exists": True}}) db.laureates.distinct("prizes.category") {'_id': ObjectId('5baacf97f35b632bbe12c1ad'), ['physics', 'chemistry', 'peace', 'born': '0000-00-00', 'medicine', 'literature', 'economics'] 'died': '0000-00-00', 'firstname': ('Comité international de la Croix Rouge' ['physics', 'chemistry', 'peace', 'medicine', 'literature', 'economi '(International Committee of the Red Cross)'), 'gender': 'org', 'id': '482', 'prizes': [{'affiliations': [[]], 'category': 'peace', 'share': '1', 'year': '1917'}, {'affiliations': [[]], 'category': 'peace', 'share': '1', 'year': '1944'}, {'affiliations': [[]], 'category': 'peace', 'share': '2', 'year': '1963'}]} INTRODUCTION TO MONGODB IN PYTHON

  5. Let's practice! IN TRODUCTION TO MON GODB IN P YTH ON

  6. Pre-�ltering distinct values IN TRODUCTION TO MON GODB IN P YTH ON Donny Winston Instructor

  7. Awards into prize shares db.laureates.find_one({"prizes.share": "4"}) {'bornCountry': 'France', 'died': '1906-04-19', 'diedCountry': 'France',, 'firstname': 'Pierre', 'prizes': [{'affiliations': [{'city': 'Paris', 'country': 'France', 'name': ('École municipale de physique et de chimie' 'industrielles (Municipal School of Industrial' 'Physics and Chemistry)')}], 'motivation': ('"in recognition of the extraordinary' 'services they have rendered by their' 'joint researches on the radiation' 'phenomena discovered by Professor' 'Henri Becquerel"'), 'category': 'physics', 'share': '4', 'year': '1903'}], 'surname': 'Curie', ... } INTRODUCTION TO MONGODB IN PYTHON

  8. High-share prize categories db.laureates.distinct("prizes.category") db.laureates.distinct( "prizes.category", {"prizes.share": '4'}) ['physics', 'chemistry', 'peace', 'medicine', 'literature', 'economics'] ['physics', 'chemistry', 'medicine'] list(db.laureates.find({"prizes.share": "4"})) db.prizes.distinct("category", {"laureates.share": "4"}) [...] ['physics', 'medicine', 'chemistry'] INTRODUCTION TO MONGODB IN PYTHON

  9. Prize categories with multi-winners db.laureates.count_documents({"prizes.1": {"$exists": True}}) 6 db.laureates.distinct( "prizes.category", {"prizes.1": {"$exists": True}}) ['chemistry', 'physics', 'peace'] # We'll learn how to do this in the next chapter: [[{'category': 'physics'}, {'category': 'chemistry'}], [{'category': 'physics'}, {'category': 'physics'}], [{'category': 'chemistry'}, {'category': 'peace'}], [{'category': 'chemistry'}, {'category': 'chemistry'}], [{'category': 'peace'}, {'category': 'peace'}, {'category': 'peace'}], [{'category': 'peace'}, {'category': 'peace'}]] INTRODUCTION TO MONGODB IN PYTHON

  10. Practice time! IN TRODUCTION TO MON GODB IN P YTH ON

  11. Matching array �elds IN TRODUCTION TO MON GODB IN P YTH ON Donny Winston Instructor

  12. Array �elds and equality {'firstname': 'John', 'surname': 'Bardeen', 'prizes': [{ 'category': 'physics', 'year': '1956', 'share': '3', 'motivation': ('"for their researches on semiconductors and their' 'discovery of the transistor effect"'), ... }, { 'category': 'physics', 'year': '1972', 'share': '3', 'motivation': ('"for their jointly developed theory of' 'superconductivity, usually called the BCS-theory"'), ... }], ... } db.laureates.count_documents({"prizes.category": "physics"}) 206 INTRODUCTION TO MONGODB IN PYTHON

  13. Array �elds and equality, simpli�ed # Imaginary extra field in John Bardeen's document: {"nicknames": ["Johnny", "JSwitch", "JB". "Tc Johnny", "Bardy"]} db.laureates.find({"nicknames": "JB"}) # different than {"nicknames": ["JB"]} INTRODUCTION TO MONGODB IN PYTHON

  14. Array �elds and operators db.laureates.count_documents( db.laureates.count_documents({ {"prizes.category": "physics"}) "prizes.category": { "$in": ["physics", "chemistry", "medicine"]}}) 206 596 db.laureates.count_documents( {"prizes.category": {"$ne": "physics"}}) db.laureates.count_documents({ "prizes.category": { "$nin": ["physics", "chemistry", "medicine"]}}) 716 326 326 INTRODUCTION TO MONGODB IN PYTHON

  15. Enter $elemMatch db.laureates.count_documents({ db.laureates.count_documents({ "prizes": { "prizes": {"$elemMatch": "category": "physics", "share": "1"}}) {"category": "physics", "share": "1"}}}) 0 47 db.laureates.count_documents({ db.laureates.count_documents({ "prizes.category": "physics", "prizes.share": "1"}) "prizes": {"$elemMatch": { "category": "physics", "share": "1", 48 "year": {"$lt": "1945"},}}}) 29 29 INTRODUCTION TO MONGODB IN PYTHON

  16. Onward and array- ward! IN TRODUCTION TO MON GODB IN P YTH ON

  17. Distinct As You Like It: Filtering with Regular Expressions IN TRODUCTION TO MON GODB IN P YTH ON Donny Winston Instructor

  18. Finding a substring with $regex db.laureates.find_one({"firstname": "Marie"}) db.laureates.distinct("bornCountry", {"bornCountry": {"$regex": "Poland"}}) {'born': '1867-11-07', 'bornCity': 'Warsaw', ['Russian Empire (now Poland)', 'bornCountry': 'Russian Empire (now Poland)', 'Prussia (now Poland)', 'firstname': 'Marie', 'Germany (now Poland)', 'surname': 'Curie, née Sklodowska', 'Austria-Hungary (now Poland)', ...} 'German-occupied Poland (now Poland)', 'Poland', 'Poland (now Ukraine)', 'Poland (now Lithuania)', 'Poland (now Belarus)', 'Free City of Danzig (now Poland)'] INTRODUCTION TO MONGODB IN PYTHON

  19. Flag options for regular expressions case_sensitive = db.laureates.distinct( import re "bornCountry", {"bornCountry": {"$regex": "Poland"}}) db.laureates.distinct("bornCountry", {"bornCountry": re.compile("poland", re.I)}) case_insensitive = db.laureates.distinct( "bornCountry", ['Russian Empire (now Poland)', ...] {"bornCountry": {"$regex": "poland", "$options": "i"}}) assert set(case_sensitive) == set(case_insensitive) from bson.regex import Regex db.laureates.distinct("bornCountry", {"bornCountry": Regex("poland", "i")}) ['Russian Empire (now Poland)', ...] INTRODUCTION TO MONGODB IN PYTHON

  20. Beginning and ending (and escaping) from bson.regex import Regex db.laureates.distinct( "bornCountry", db.laureates.distinct("bornCountry", {"bornCountry": Regex("now Poland\)$")}) {"bornCountry": Regex("^Poland")}) ['Russian Empire (now Poland)', ['Poland', 'Prussia (now Poland)', 'Poland (now Ukraine)', 'Germany (now Poland)', 'Poland (now Lithuania)', 'Austria-Hungary (now Poland)', 'Poland (now Belarus)'] 'German-occupied Poland (now Poland)', 'Free City of Danzig (now Poland)'] db.laureates.distinct( "bornCountry", {"bornCountry": Regex("^Poland \(now")}) ['Poland (now Ukraine)', 'Poland (now Lithuania)', 'Poland (now Belarus)'] INTRODUCTION TO MONGODB IN PYTHON

  21. Let's practice! IN TRODUCTION TO MON GODB IN P YTH ON

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend