Guest User

Untitled

a guest
Nov 26th, 2018
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import pymongo
  3. from pprint import pprint
  4. from random import randint
  5.  
  6.  
  7. from fire import Fire
  8.  
  9.  
  10. def insert():
  11. db = pymongo.MongoClient().toto
  12.  
  13. for _ in range(100):
  14. db.toto.insert_one(
  15. {
  16. "transactions": [
  17. {"id": randint(0, 100000), "hello": randint(0, 1000)}
  18. for _ in range(100000)
  19. ]
  20. }
  21. )
  22.  
  23.  
  24. def create_index():
  25. db = pymongo.MongoClient().toto
  26. db.toto.create_index("transactions.id")
  27.  
  28.  
  29. def drop_indexes():
  30. db = pymongo.MongoClient().toto
  31. db.toto.drop_indexes()
  32.  
  33.  
  34. def drop():
  35. pymongo.MongoClient().drop_database("toto")
  36.  
  37.  
  38. def slow_query():
  39. db = pymongo.MongoClient().toto
  40.  
  41. r = list(
  42. db.toto.aggregate(
  43. [
  44. {"$match": {"transactions.id": 17}},
  45. {"$unwind": "$transactions"},
  46. {"$match": {"transactions.id": 17}},
  47. ]
  48. )
  49. )
  50. pprint(r[:10])
  51. return len(r)
  52.  
  53.  
  54. def query():
  55. db = pymongo.MongoClient().toto
  56.  
  57. r = list(
  58. db.toto.aggregate(
  59. [
  60. {"$match": {"transactions.id": 17}},
  61. {
  62. "$addFields": {
  63. "transactions": {
  64. "$filter": { # We override the existing field!
  65. "input": "$transactions",
  66. "as": "comment",
  67. "cond": {"$eq": ["$transaction.id", 17]},
  68. }
  69. }
  70. }
  71. },
  72. ]
  73. )
  74. )
  75. pprint(r[:10])
  76. return len(r)
  77.  
  78.  
  79. def new_query():
  80. db = pymongo.MongoClient().toto
  81.  
  82. r = list(
  83. db.toto.aggregate(
  84. [
  85. {"$match": {"transactions.id": 17}},
  86. {
  87. "$project": {
  88. "transactions": {
  89. "$filter": {
  90. "input": "$transactions",
  91. "as": "transaction",
  92. "cond": {"$eq": ["$$transaction.id", 17]},
  93. }
  94. }
  95. }
  96. },
  97. {"$unwind": "$transactions"},
  98. ]
  99. )
  100. )
  101. pprint(r[:10])
  102. return len(r)
  103.  
  104.  
  105. def test_query():
  106. db = pymongo.MongoClient().toto
  107.  
  108. r = list(db.toto.find({"transactions.id": 17}))
  109. pprint(r[:10])
  110. return len(r)
  111.  
  112.  
  113. if __name__ == "__main__":
  114. Fire()
Advertisement
Add Comment
Please, Sign In to add comment