Notes for working with Mongo and Eve
1. Install Mongo, according to http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/
brew install mongodb
Some Mongo Resources
-
mongoHub-Mac -- use for examining database directly
2. create a local database:
mkdir /Users/houpt/Documents/jedi/data/db
3. Install Python 2.7
brew install python
4. Install Eve
pip install eve
5. Be sure that path is set correctly in .bash_profile to run things installed with homebrew and pip (e.g. python 2.7, eve) by adding the following line to the end of /Users/Houpt/.bash_profile:
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
6. Run MongoDB
mongod --dbpath /Users/houpt/Documents/jedi/data/db
to persisting run mongodb, and launch it at startup, see http://stackoverflow.com/questions/5596521/what-is-the-correct-way-to-start-a-mongod-service-on-linux-os-x:
/usr/local/Cellar/mongodb/3.0.2/homebrew.mxcl.mongodb.plist
copy into
~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
To add the file to the launchd service:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
To start the launchd
launchctl start homebrew.mxcl.mongodb.plist
7. Run Eve in grad/eve (be sure to start Mongo first)
python run.py
Alternatively, just launch eve.command
Now you can see the DB at:
The basic CRUD example is at:
http://127.0.0.1:5000/eve/studentapp.html
Add a student:
curl -d '{"full_name": "test", "list": [1,2,3]}' -H 'Content-Type: application/json' http://127.0.0.1:5000/api/students
Reset collection by Deleting ALL Students (DANGEROUS) (* REMEMBER TO RENAME the updated allstudents.json to something like allstudentsUPDATED.json SO IT DOESN'T GET OVERWRITTEN by Mongo-Padawan DB')
curl -X DELETE http://127.0.0.1:5000/api/students
Upload all current students (assumes Jekyll Serve is running)
curl http://127.0.0.1:4000/eve/allstudents.json > s.json
curl -d @s.json -H 'Content-Type: application/json' http://127.0.0.1:5000/api/students
or if the allstudents.json is okay...
BE SURE TO REMOVE the "_id" fields from the json before uploading into MongoDB
( be sure to see note before: better to use mongoexport / mongoimport )
Here is a regex to search for the mongodb _id
field. Just replace with \n
\n\s+\"_id\":\s+\"[a-zA-Z0-9]+\"\s*,\n
...then upload allstudents to the database "students"
curl -d @allstudentsUPDATED_no_ids.json -H 'Content-Type: application/json' http://127.0.0.1:5000/api/students
Download students into the _data
directory of Jekyll:
(note use of jq to extract _items. Install via brew install jq
)
curl http://127.0.0.1:5000/api/students | jq '._items' > _data/allstudents_old_format.json
For better SVN diffs, sort records by FSU-ID, and sort all keys alphabetically:
curl http://127.0.0.1:5000/api/students | jq --sort-keys '._items | sort_by(.fsuid)' > _data/allstudents.json
## Export and Import Via Mongo Utilities
Can also use mongoexport to export the db as a json file. Assuming the database is jedi, and collections is students:
mongoexport -d jedi -c students -o exported_students.json --pretty
To reimport use mongoimport -- note that documents (records) in the db will be replaced if the _id field matches: "With --mode upsert, mongoimport replaces existing documents in the database that match a document in the import file with the document from the import file. Documents that do not match an existing document in the database are inserted as usual. By default mongoimport matches documents based on the _id field. " mongoimport docs
mongoimport -c students -d jedi --mode upsert --file exported_students_fixed.json
August 13 2018: For reference, I believe this is how to do the full round-trip export/import with new _id's:
mongoexport --db jedi --collection students > tmp_students.json
jq 'del(._id)' tmp_students.json | mongoimport --drop --db jedi --collection students
The jq call deletes the old _id's on the fly.