Skip to content

Instantly share code, notes, and snippets.

@sofianinho
Created November 21, 2016 16:56
Show Gist options
  • Save sofianinho/4e4f69ce12733c364a76209032e5d80f to your computer and use it in GitHub Desktop.
Save sofianinho/4e4f69ce12733c364a76209032e5d80f to your computer and use it in GitHub Desktop.
Change Mongo entry on the fly
# Mongo database musty be started with the rest option
# Port 28017 is the port for restful HTTP calls
docker run --rm -it mongo:3.2 --rest
#Suppose you injected the following
curl -i -X POST -d '{ "firstName" : "Rick", "lastName" : "Sanchez" }' http://172.17.0.3:28017/testDB/testC
#Return one entry with firstName equal Rick
curl -i 'http://172.17.0.3:28017/testDB/testC?filter_firstName=Rick&limit=1'
#Modify it using jq and sed
curl -s 'http://172.17.0.3:28017/testDB/testC?filter_firstName=Rick&limit=1' |jq .'rows[0]'|sed 's/\"lastName\".\+$/\"lastName\": \"Wabba lubba dub dub\"/g'
# To reinject the modified content, you need to localize the "_id" generated by Mongo and delete it to add the entry. For example, if it's line 2 to 4, with sed
curl -s 'http://172.17.0.3:28017/testDB/testC?filter_firstName=Rick&limit=1' |jq .'rows[0]'|sed 's/\"lastName\".\+$/\"lastName\": \"Wabba lubba dub dub\"/g' |sed 2,4d
# Then, the complete resulting command
curl -s 'http://172.17.0.3:28017/testDB/testC?filter_firstName=Rick&limit=1' |jq .'rows[0]'|sed 's/\"lastName\".\+$/\"lastName\": \"Wabba lubba dub dub\"/g' |sed 2,4d|curl -X POST -d @- http://172.17.0.3:28017/testDB/testC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment