Connect to MongoDB and count the total number of documents in a MongoDB query on a collection in the database. Here, each document in the
collection represents an employee.
Create a MongoDB connection to the database mongotest
. Here, the database server dbtb01
hosts this database using port number 27017
.
conn =
mongo with properties:
Database: 'mongotest'
UserName: ''
Server: {'dbtb01'}
Port: 27017
CollectionNames: {'airlinesmall', 'employee', 'largedata' ... and 3 more}
TotalDocuments: 23485919
conn
is the
mongo
object that contains the MongoDB connection. The object properties contain information about the connection and the
database.
The database name is mongotest
.
The user name is blank.
The database server is dbtb01
.
The port number is 27017
.
This database contains six document collections. The first three collection names are
airlinesmall
, employee
, and
largedata
.
This database contains 23,485,919 documents.
Verify the MongoDB connection.
The database connection is successful because the isopen
function returns 1
. Otherwise, the database connection is closed.
Create a JSON-style query as a character vector that contains a JSON-style
string. This query sets the department
field equal to the
value Sales
.
Use the MongoDB query on the employee
collection to count
the total number of employees who work in the Sales department. A total of
four employees work in the Sales department.
Close the MongoDB connection.