Guest User

single connection

a guest
Oct 14th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. Guys,
  2.  
  3. I want to modify this script so that it does not open mongo connection in each loop
  4.  
  5. # cat names.txt
  6. Robert
  7. Salim
  8. James
  9. Kolja
  10. Manfred
  11.  
  12. *********************8
  13. #!/bin/bash
  14.  
  15. MONGO_DBUSER='admin'
  16. MONGO_DBPASS='admin123'
  17. MONGO_SLAVE_SERVER='192.168.1.5'
  18. MONGO_DB='students'
  19. MONGO_COLLECTION='college'
  20.  
  21. while read -r names; do
  22. $(which mongo) --host=$MONGO_SLAVE_SERVER -u $MONGO_DBUSER -p $MONGO_DBPASS --authenticationDatabase admin --port 27017 --quiet $MONGO_DB <<EOF
  23. rs.slaveOk()
  24. db.${MONGO_COLLECTION}.find({"name" : "${names}"},{name: 1, location: 1, _id: 0});
  25. EOF
  26. done < names.txt | perl -ne 'print "$1,$2\n" if /"name" : "([^"].*)", "location" : "([^"].*)"/' > name_location.csv
  27. ************************
  28.  
  29. so I tried this but it is not working. csv file is not generating
  30.  
  31. *************************************
  32. #!/bin/bash
  33.  
  34. MONGO_DBUSER='admin'
  35. MONGO_DBPASS='admin123'
  36. MONGO_SLAVE_SERVER='192.168.1.5'
  37. MONGO_DB='students'
  38. MONGO_COLLECTION='college'
  39.  
  40. exec 4> >(mongo --host=$MONGO_SLAVE_SERVER --port 27017 --quiet $MONGO_DB)
  41.  
  42. while read -r names; do
  43. echo "db.${MONGO_COLLECTION}.find({"names" : \"${names}\"},{names: 1, location: 1, _id: 0})" >&4
  44. done < names.txt | perl -ne 'print "$1,$2\n" if /"name" : "([^"].*)", "location" : "([^"].*)"/' > name_location.csv
  45.  
  46. **********************************
Add Comment
Please, Sign In to add comment