mongo_shell()

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

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 a result value..
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.

Parameters:

fd

MongoDB connection object

s 

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

Return value:
 
A table sequence/cursor

Example:

 

A

 

1

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

Conncet to database mydb on Mongo server

2

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

Query records of the emp set in database mydb and return result as a table sequence

 

3

=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

4

=A3.fetch()

 

5

=mongo_shell@x(A1,"{'count':'emp'}")

Use @x option to query the number of records, and close the connection after result is returned

 

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(A1)

Close the database connection