mongo_shell()

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

Description:
Query MongoDB database and return a table sequence.

Syntax:
mongo_shell(fd,s)

Note:

The external library function (See External Library Guide) queries data in a MongoDB database 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.

Parameter:

fd

MongoDB connection object

s 

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

Return value:
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 all records of the emp set and return a table sequence.

 

4

=mongo_shell(A1,"{'find':'emp',filter:{GENDER:'F'}}")

Query records where GENDER is F and return a cursor.

5

=A4.fetch()

 

6

=mongo_shell@d(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,"{'count':'emp'}")

Count records in emp:

8

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

Get unique DEPT field values from emp.

9

=mongo_shell@d(A1,"{'aggregate':'emp',pipeline: [{'$group':{'_id':'$DEPT','count':{'$sum':1}}}],cursor: { }}")

 

Compute the number of employees in each department:

10

=mongo_close(A1)

Close the database connection.