cklanac
7/9/2018 - 5:22 PM

Challenge 11: Mongo Basics

Challenge 11: Mongo Basics

For this challenge you will run through a set of CRUD operation with Mongo.

Getting Started

To get started, you will need to create a noteful database with a notes collection and populate it with documents.

  • Copy the contents of this gist into a JSON file and import it using mongoimport. Note, the data in ths gist is formatted as a proper JSON array of objects, so the import process is slightly different that the Restaurants data used earlier. Below is the mongoimport command with --jsonArray flag.
mongoimport -d noteful -c notes --drop --jsonArray --file ~/<path-to-unzipped-data-file>

Next, create a mongo-queries.js file. In the file, create the queries to solve each of the following:

Exercises

  1. Write a MongoDB query to display all the documents in the collection notes.

  2. Write a MongoDB query to display all the documents in the collection notes and format the results to be 'pretty'.

  3. Write a MongoDB query to display the fields title and content for all the documents in the collection notes.

  4. Write a MongoDB query to display the fields title and content but exclude the field _id for all the documents in the collection notes.

  5. Write a MongoDB query to display only the title field for all the documents in the collection notes and sort the results by _id in descending order.

  6. Write a MongoDB query to display all the documents from the collection notes which contain the title '5 life lessons learned from cats'.

  7. Write a MongoDB query to display the first 5 documents from the collection notes.

  8. Write a MongoDB query to display the next 5 documents from the collection notes after skipping the first 5.

  9. Write a MongoDB query to display the total number of documents in the collection notes.

  10. Write a MongoDB query to display the documents from the collection notes which have an _id that is greater than "000000000000000000000007".

  11. Write a MongoDB query to display the documents from the collection notes which have an _id which is greater than or equal to "000000000000000000000009" but less than or equal to "000000000000000000000017".

  12. Write a MongoDB query to display the documents from the collection notes which have an _id which is less than or equal to "000000000000000000000007".

  13. Write a MongoDB query to display only one document from the collection notes.

  14. Write a MongoDB query to display only the title of one document from the collection notes (_id can be included).

  15. Write a MongoDB query to display only the title of one document from the collection notes (_id excluded).

  16. Write a MongoDB query to insert one document into the collection notes. The title and content fields can be whatever you like.

  17. Write a MongoDB query to insert two note documents into the collection notes. The title and content fields can be whatever you like.

  18. Write a MongoDB query to modify the title and content fields of the document from the collection notes with _id "000000000000000000000003". Change the title and content to be whatever you like.

  19. Write a MongoDB query to modify only the title field of the document from the collection notes with _id "000000000000000000000007". The content field should remain unchanged.

  20. Write a MongoDB query to modify the title and content fields of all the documents in the collection notes that have an _id field greater than "000000000000000000000014".

  21. Write a MongoDB query to remove only the title field from the document in the collection notes with _id "000000000000000000000008".

  22. Write a MongoDB query to remove the content fields from all documents in the collection notes with _id less than or equal to "000000000000000000000006".

  23. Write a MongoDB query to remove the title fields from all documents in the collection notes with _id less than or equal to "000000000000000000000003".

  24. Write a MongoDB query to remove the document from the collection notes that has an _id "000000000000000000000017".

  25. Write a MongoDB query to remove the documents from the collection notes that have an _id which is not less than "000000000000000000000018".

  26. Write a MongoDB query to remove the documents from the collection notes that have an _id which is greater than or equal to "000000000000000000000013" and contain the string 'dogs' in the title.

  27. Write a MongoDB query to display all the documents from the collection notes which do not have a title field.

  28. Write a MongoDB query to remove all the documents from the collection notes which contain the string 'cat' in the title but not the string 'the'.

  29. Write a MongoDB query to display all the documents from the collection notes that have a title field which does not contain the string 'dogs' and does contain a title field.