Description:
Query
and manipulate the databsase and return a result value.
Syntax:
mongo_shell(fd,s)
Note:
The external library function maniputlates data in a MongoDB
database through the db.runCommand() interface and returns the table sequence
in the outermost layer..
Options:
@x |
Close the MongoDB connection automatically closes when the execution finishes; if the returned result is a cursor, disconnect from the database as the cursor closes. |
@d |
Return a table sequence |
@c |
Return a cursor; should be used together with @d option, otherwise it is invalid |
Parameters:
fd |
MongoDB connection object |
s |
A JSON string that is equivlalent to parameter str in db.runCommand(str) |
Return value:
The outermost table sequence
Example:
|
A |
|
1 |
=mongo_open("mongodb://localhost:27017/mydb") |
Conncet to database mydb on Mongo server |
2 |
=mongo_shell(A1,"{'find':'emp'}")
|
Return the outermost table sequence |
3 |
=mongo_shell(A1,"{'find':'emp'}") |
Query records of the emp set in database mydb and return result as a table sequence
|
4 |
=mongo_shell(A1,"{'find':'emp',batchSize:2,filter:{GENDER:'F'}}") |
Query records where GENDER is F, with each query batch returning two records, and return result as a cursor |
5 |
=A4.fetch() |
|
6 |
=mongo_shell@x(A1,"{'count':'emp'}") |
Use @x option to query the number of records, and close the connection after result is returned
|
7 |
=mongo_shell(A1,"{'find':'emp',projection:{NAME:1,DEPT:1,SALARY:1},sort:{SALARY:1}}") |
Display a table sequence consisting of NAME field, DEPT field and SALARY field and sorted by SALARY in ascending order |
8 |
=mongo_shell(A1,"{'insert':'emp',documents:[{_id:1,NAME:'Lily',GENDER:'M',DEPT:'Sale',SALARY:13500}]}") |
Insert data to emp |
9 |
=mongo_shell(A1,"{'findAndModify':'emp',query:{'NAME':'Lily'},update:{'$set':{SALARY:10000}}}") |
Modify SALARY in emp’s record where NAME is Lily to 10000
|
10 |
=mongo_shell(A1,"{'distinct':'emp',key:'DEPT'}") |
Get unique DEPT field values from emp |
11 |
=mongo_close(A1) |
Close the database connection |