mongo_shell()

Read(2852) Label: query, mongodb database, result,

Description:
Query and manipulate the database and return a result value.

Syntax:
   mongo_shell(fd,s)

Note:

The external library function (See External Library Guide) manipulates data in a MongoDB database through the db.runCommand() interface and returns the table sequence in the outermost layer..
Option:

@e

Go on with the execution when error appears and return the error information

@d

Return a table sequence

@c

Return a cursor; should be used together with @d option, otherwise it is invalid

Parameter:

fd

MongoDB connection object

s 

A JSON string that is equivalent to parameter str in db.runCommand(str)

Return value:
 
The outermost table sequence

Example:

 

A

 

1

=mongo_open("mongodb://localhost:27017/mydb")

Connect 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(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.

7

=mongo_shell(A1,"{'insert':'emp',documents:[{_id:1,NAME:'Lily',GENDER:'M',DEPT:'Sale',SALARY:13500}]}")

Insert data to emp.

8

=mongo_shell(A1,"{'findAndModify':'emp',query:{'NAME':'Lily'},update:{'$set':{SALARY:10000}}}")

Modify SALARY in emp’s record where NAME is Lily to 10000.

 

9

=mongo_shell(A1,"{'distinct':'emp',key:'DEPT'}")

Get unique DEPT field values from emp.

10

=mongo_close()

Close the database connection.