These are examples using cURL
export mongoser="http://yourmongohost.com"
There two options to pass database name and collection name
One is to pass it as parameters
For example
curl -i "http://yourmongohost.com/query?dbname=mydbname&colname=mycolname"
or you can pass it as part of path
for example
curl -i "http://yourmongohost.com/query/mydbname/mycolname"
---------------------
Put new file to gridfs
curl -i "http://yourmongohost.com/gridfs/db1/bucket1?filename=myimage.jpeg" -XPUT --data-binary @myimage.jpeg -H"Content-Type:image/jpeg"
or
curl -i "http://yourmongohost.com/gridfs?dbname=db1&bucketname=bucket1&filename=myimage.jpeg" -XPUT --data-binary @myimage.jpeg -H"Content-Type:image/jpeg"
Replace file to gridfs
curl -i "http://yourmongohost.com/gridfs/db1/bucket1?filename=myimage.jpeg" -XPOST --data-binary @myimage.jpeg
Delete file from gridfs
Replace file to gridfs
curl -i "http://yourmongohost.com/gridfs/db1/bucket1?filename=myimage.jpeg" -XDELETE
Get file from gridfs
Replace file to gridfs
curl -i "http://yourmongohost.com/gridfs/db1/bucket1?filename=myimage.jpeg"
List files in bucket
Replace file to gridfs
curl -i "http://yourmongohost.com/gridfs/db1/bucket1?op=list"
---------------------
When REST server auth turned on - access to mongodb is based on user roles
if you specify auth=true, you need to add users to auth.properties and put them in
one of three roles: mongoreadonly, mongoreadwrite, admin
suppose this is the line in auth.properties
admin: aaaaaa, admin
Then if you want drop db
curl -i "$mongohost/database?dbname=foo" -XDELETE -u"admin:aaaaa"
---------------------
When Mongodb started with --auth option you can authenticate to particular database passing 2
additional params: user amd passwd
For example
curl -i "$mongohost/write?dbname=foo&colname=test&user=simpleuser&passwd=mypasswd" -XPUT --data-binary '{name:"foo"}'
use -i to see headers
list databases
curl -i "$mongohost/database"
drop database
curl -i "$mongohost/database?dbname=foo" -XDELETE
get db stat
curl -i "$mongohost/database?dbname=foo&op=stats"
list collections
curl -i "$mongohost/collection?dbname=foo"
get col stat
curl -i "$mongohost/collection?dbname=foo&op=stats&colname=test"
create col
curl -i "$mongohost/collection?dbname=foo&colname=test" -XPUT
add record
curl -i "$mongohost/write?dbname=foo&colname=test" -XPUT --data-binary '{name:"foo"}'
add multi record must be separated by newline
curl -i "$mongohost/write?dbname=foo&colname=test" -XPUT --data-binary $'{name:"foo"}\n{name:"moo"}\n{name:"cow"}'
update record, first field is condition, must be separated by newline
curl -i "$mongohost/write?dbname=foo&colname=test" -XPOST --data-binary $'{name:"foo"}\n{name:"newfoo"}'
takes two optional params:upsert and multi, they are false by default
curl -i "$mongohost/write?dbname=foo&colname=test&upsert=false&multi=false" -XPOST --data-binary $'{name:"foo"}\n{name:"newfoo"}'
delete records with condition
curl -i "$mongohost/write?dbname=foo&colname=test" -XDELETE -d '{name:"newfoo"}'
read all records in collection
curl -i "$mongohost/query?dbname=foo&colname=test"
with limit
curl -i "$mongohost/query?dbname=foo&colname=test&limit=3"
with limit and skip
curl -i "$mongohost/query?dbname=foo&colname=test&limit=3&skip=3"
with condition
curl -i "$mongohost/query?dbname=foo&colname=test" -XPOST -d'{name:"newfoo"}'
with condition and sort
curl -i "$mongohost/query?dbname=foo&colname=test" -XPOST --data-binary $'{name:"newfoo"}\n{name:1}'
only return certain fields
curl -i "$mongohost/query?dbname=foo&colname=test&fields=name,age"
#distinct by key
curl -i "$mongohost/distinct?dbname=foo&colname=test&key=name"
#distinct by key with query
curl -i "$mongohost/distinct?dbname=foo&colname=test&key=name" -XPOST -d'{name:"newfoo"}'
list indexes
curl -i "$mongohost/index?dbname=foo&colname=test"
add index
curl -i "$mongohost/index?dbname=foo&colname=test" -XPUT -d"{name:1,age:1}"
delete index
curl -i "$mongohost/index?dbname=foo&colname=test" -XDELETE -d"{name:1,age:1}"
lucene searching
simple one field word search
curl -i "$mongohost/search?dbname=foo&colname=test&field=foo&text=word"
simple one field word search with number of top search items to return, defaults to 10
curl -i "$mongohost/search?dbname=foo&colname=test&field=foo&text=word&limit=22"
please read lucene query syntax here http://lucene.apache.org/core/3_6_0/queryparsersyntax.html
curl -i "$mongohost/search?dbname=foo&colname=test&limit=22" -XPOST -d'word1 AND word2 OR anotherword'
search index flushed to disk when lucene buffer is full or there were X documents changed
before search index committed it can not be searched
to commit search index modifications manually
curl -i "$mongohost/search?op=commit"