MongoDB Basics: Chapter 4: Advanced CRUD Operations - Array Operators and Projection


No Action
1
MongoDB Basics: Chapter 4: Advanced CRUD Operations - Array Operators and Projection

0 comment

21 December 2020, 9:57 am by antelove19

MongoDB Basics: Chapter 4: Advanced CRUD Operations - Array Operators and Projection
MongoDB Basics: Chapter 4: Advanced CRUD Operations - Array Operators and Projection

Commands used in this lesson: Connect to the Atlas cluster: ```javascript mongo "mongodb+srv://:@.mongodb.net/admin" ```
Switch to this database: ```javascript use sample_airbnb ```
Find all documents with exactly 20 amenities which include all the amenities listed in the query array, and display their price and address: ```javascript db.listingsAndReviews.find({ "amenities": { "$size": 20, "$all": [ "Internet", "Wifi", "Kitchen", "Heating", "Family/kid friendly", "Washer", "Dryer", "Essentials", "Shampoo", "Hangers", "Hair dryer", "Iron", "Laptop friendly workspace" ] } }, {"price": 1, "address": 1}).pretty() ```
Find all documents that have Wifi as one of the amenities only include price and address in the resulting cursor: ```javascript db.listingsAndReviews.find({ "amenities": "Wifi" }, { "price": 1, "address": 1, "_id": 0 }).pretty() ```
Find all documents that have Wifi as one of the amenities only include price and address in the resulting cursor, also exclude ``"maximum_nights"``. **This will be an error:* ```javascript db.listingsAndReviews.find({ "amenities": "Wifi" }, { "price": 1, "address": 1, "_id": 0, "maximum_nights":0 }).pretty() ```

Keywords:



Sources:

  1. university.mongodb.com