mongo

Allows you to browse MongoDB or transmit the input records to the file.

Syntax

mongo PROFILE [database=DB_NAME] SUBCOMMAND
mongo PROFILE [database=DB_NAME] MONGODB_METHOD
Required Parameters
PROFILE
MongoDB connection profile. You can configure the profile in the web console.
SUBCOMMAND
Command to be executed in the MongoDB session: dbs, cols
  • cols: Lists collections for the database specified by the database=DB_NAME option. If no database is specified, all collections from all databases are shown.
  • dbs: List all databases in MongoDB. Used alone without any other arguments.
MONGO_METHOD
MongoDB native method. For the collection specified by COL_NAME, it receives an input record or expression as a parameter argument and executes the method.
Optional Parameters
database=DB_NAME
Database on which to run the cols command.

Description

Output fields by SUBCOMMAND

The output fields of the dbs command are:

  • name: Database name
  • disk_usage: Disk usage in bytes
  • empty: true if the database is empty and false otherwise.

The output fields of the cols command are:

  • name: Collection name
  • type: Type (collection)
  • options: Collection configuration options
  • info: Additional information such as whether it is read-only.
  • idIndex: _id index specification
MONGO_METHOD

The mongodb command supports the following MongoDB native methods. You can check the usage of each method in MongoDB Reference Manual.

find()
db.COL_NAME.find("FILTER_EXPR", ["PROJECTION_EXPR"])

Selects documents specified by COL_NAME in a collection and returns a cursor to the selected documents.

"FILTER_EXPR"
A JSON expression that specifies search conditions (filters) If you specify it as null, it retrieves all documents.
["PROJECTION_EXPR"]
A JSON expression that specifies the fields to be returned in the documents that match the search condition. If you omit the expression, it returns all fields in the documents that match the search condition. For more information, see "Projection" in MongoDB Documentation.

For information on query operators available in FILTER_EXPR and PROJECTION_EXPR, refer to the "Query and Projection Operators" in the MongoDB Documentation.

insert()
db.COL_NAME.insert()

Inserts a document or documents into a collection specified by COL_NAME. If the document contains an _id value, it is used as a unique identification key (ObjectId). The _id values must be unique. If the document does not specify an _id field, then MongoDB adds a 12-byte hexadecimal unique identification key for the document before inserting. See usage #4, #5.

updateOne()
db.COL_NAME.updateOne("KEY", "UPDATE")

Searches the collection specified by COL_NAME with the value of the KEY field and modifies the value of the UPDATE field in the first document returned in the collection. See usage #6.

When using the _id field as KEY, if the unique identifier is an automatically generated 12-digit binary, the _id value MUST be set to the binary type.

updateMany()
db.COL_NAME.updateMany("KEY_LIST", "UPDATE_LIST")

Searches the collection specified by COL_NAME with the value of the KEY_LIST field and modifies the value of the UPDATE_LIST field in all the documents returned. Both KEY_LIST and UPDATE_LIST use a comma (,) as a separator. Unlike the db.COL_NAME.updateOne() method, this modifies all searched documents.

deleteOne()
db.COL_NAME.deleteOne("KEY")

Searches the collection specified by COL_NAME with the value of the KEY field and deletes the first document returned in the collection.

When using the _id field as KEY, if the unique identifier is an automatically generated 12-digit binary, the _id value MUST be set to the binary type.

deleteMany()
db.COL_NAME.deleteMany("KEY_LIST")

Searches the collection specified by COL_NAME with the value of the KEY_LIST field and removes all documents. KEY_LIST uses a comma (,) as a separator.

Usage

  1. Retrieve all documents in the inventory collection.

    mongo PROFILE db.inventory.find()
    
  2. Retrieve documents with 3 or more stars from the restaurants collection.

    mongo PROFILE db.restaurants.find("{stars: {$gte: 3}}")
    
  3. Output only the name and stars fields from the restaurants collection.

    mongo PROFILE db.restaurants.find(null, "{name: true, stars: true}")
    
  4. Provide an arbitrary JSON document into the MongoDB restaurants collection.

    json "{
      name: Café Con Leche,
      contact: {
        phone: 228-555-0149,
        email: cafeconleche@example.com,
        location: [-73.92502, 40.8279556]
      },
      stars:3,
      categories: [Bakery, Coffee, Pastries]
    }"
    | mongo PROFILE db.restaurants.insert()
    
  5. Provide the lastest 10 items of the Logpresso inventory table into the MongoDB inventory collection.

    table limit=10 inventory
    | mongo PROFILE db.inventory.insert()
    
  6. Search the restaurants collection based on the name field and modify the stars value.

    json "{}" 
    | eval name="Café Con Leche", stars=4 
    | mongo PROFILE db.restaurants.updateOne("name", "stars")
    
  7. Search 1 document with an ObjectId of 5982df1b7098262f64d4ffaf and then delete it.

    json "{}" 
    | eval _id = fromhex("5982df1b7098262f64d4ffaf") 
    | mongo stream2 db.restaurants.deleteOne("_id")