Difference between revisions of "MongoDB JS Files"

From mi-linux
Jump to navigationJump to search
 
(13 intermediate revisions by the same user not shown)
Line 13: Line 13:
 
If you do not know how to use '''vi''' then it is probably easier if you use something like '''NotePad++''' on your PC to create,or amend the file, then use a secure file transfer protocol (sftp) programme to transfer it to mi-linux using a client, such as '''FireZilla'''.
 
If you do not know how to use '''vi''' then it is probably easier if you use something like '''NotePad++''' on your PC to create,or amend the file, then use a secure file transfer protocol (sftp) programme to transfer it to mi-linux using a client, such as '''FireZilla'''.
  
=== Using FTP ===
+
=== Using Secure FTP ===
  
 
Note when using a FTP client:
 
Note when using a FTP client:
Line 19: Line 19:
 
* the full hostname is: '''mi-linux.wlv.ac.uk''' (or '''csl-student.wlv.ac.uk''')
 
* the full hostname is: '''mi-linux.wlv.ac.uk''' (or '''csl-student.wlv.ac.uk''')
 
* the login and username is your normal University username and password
 
* the login and username is your normal University username and password
 +
 +
See [[https://mi-linux.wlv.ac.uk/wiki/index.php/%28S%29FTP https://mi-linux.wlv.ac.uk/wiki/index.php/(S)FTP]] and the section '''Using FireZilla''' for further information.
  
 
=== Create a file ===
 
=== Create a file ===
Line 24: Line 26:
 
Use a text editor you are familiar with and type in the following:
 
Use a text editor you are familiar with and type in the following:
  
  db.deptCollection.find({deptno:20}, { employees: {$elemMatch: { job: "MANAGER"}}}).pretty()
+
  printjson(db.deptCollection.find({deptno:10},
 +
  { employees: {$elemMatch: { job: "MANAGER"}}}).toArray())
  
  
Line 30: Line 33:
  
 
  myQuery.js
 
  myQuery.js
 +
 +
Note:
 +
* this file is created outside MongoDB.
 +
* '''printjson''' is needed to output the results, otherwise MongoDB will just return ''true''.
 +
* '''toArray()''' is used instead of pretty().
 +
  
 
== Run a JS File ==
 
== Run a JS File ==
Line 41: Line 50:
  
 
See the [https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/ MongoDB] manual for further details.
 
See the [https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/ MongoDB] manual for further details.
 +
  
 
== Next Step ==
 
== Next Step ==
  
Return to the [[MongoDB_Workbook|Workbook]].
+
[[MongoDB_Misc|Miscellaneous]] Commands.

Latest revision as of 12:41, 27 October 2016

Main Page >> MongoDB >>MongoDB Workbook >> JavaScript Files

Query Files

The Mongo Shell used mi-linux is an interactive JavaScript interface to MongoDB. As you have seen the mongo shell can be used to query and update your data.

So far the query examples seen are not saved for future use. The data will be stored in the database, but not the queries used.

If you wish to save a query, a text editor should be used to create a .js file. If you are familiar with vi, this can be used on mi-linux (csl-student) to create the file.

Note, these files are created outside the MongoDB environment.

If you do not know how to use vi then it is probably easier if you use something like NotePad++ on your PC to create,or amend the file, then use a secure file transfer protocol (sftp) programme to transfer it to mi-linux using a client, such as FireZilla.

Using Secure FTP

Note when using a FTP client:

  • it must be a secure connection, i.e., sftp
  • the full hostname is: mi-linux.wlv.ac.uk (or csl-student.wlv.ac.uk)
  • the login and username is your normal University username and password

See [https://mi-linux.wlv.ac.uk/wiki/index.php/(S)FTP] and the section Using FireZilla for further information.

Create a file

Use a text editor you are familiar with and type in the following:

printjson(db.deptCollection.find({deptno:10},
  { employees: {$elemMatch: { job: "MANAGER"}}}).toArray())


Save the file as:

myQuery.js

Note:

  • this file is created outside MongoDB.
  • printjson is needed to output the results, otherwise MongoDB will just return true.
  • toArray() is used instead of pretty().


Run a JS File

Within MongoDB type in:

load("myQuery.js")


Further Information

See the MongoDB manual for further details.


Next Step

Miscellaneous Commands.