pestiand82

Mongo DB

Nov 3rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from pymongo import MongoClient
  2. from bson import ObjectId
  3.  
  4. client = MongoClient("localhost", 27017)
  5. db = client["MovieLibrary"]
  6. collection = db["movies"]
  7.  
  8. # CRUD
  9.  
  10. # create data
  11. # collection.insert_one(
  12. #     {
  13. #         "title":"Terminator 2",
  14. #         "language": "eng"
  15. #      }
  16. # )
  17. #
  18. # collection.insert_one(
  19. #     {
  20. #         "title":"Amelie",
  21. #         "language": "fr"
  22. #      }
  23. # )
  24. #
  25. # collection.insert_one(
  26. #     {
  27. #         "title":"Arrival",
  28. #         "language": "eng"
  29. #      }
  30. # )
  31.  
  32. # find data all
  33. # print([i for i in collection.find()])
  34.  
  35. # find by name
  36. # print(collection.find_one({ "title": "Aliens"}))
  37.  
  38. # find by id
  39. # print(collection.find_one({ "_id": ObjectId('5dbee0fcbdb9649e9fc5b115')}))
  40.  
  41. # find by language
  42. # print([i for i in collection.find({"language":"eng"})])
  43.  
  44. # edit data
  45. # collection.update_one({"_id":ObjectId('5dbee0fcbdb9649e9fc5b115')}, {"$set":{"language":"eng"}})
  46.  
  47. # delete
  48. collection.delete_one({"_id":ObjectId('5dbee0fcbdb9649e9fc5b115')})
Add Comment
Please, Sign In to add comment