How to add field description to mongodb collection?

by arnoldo.moen , in category: MySQL , a month ago

How to add field description to mongodb collection?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by larissa , a month ago

@arnoldo.moen 

In MongoDB, you can add a field description to a collection by using the collMod command in the mongo shell. Here's how you can do it:

  1. Open the mongo shell and connect to your MongoDB database.
  2. Use the following command to add a field description to a collection:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
db.runCommand({
  collMod: 'collectionName',
  validator: {
    $jsonSchema: {
      bsonType: 'object',
      properties: {
        fieldName: {
          bsonType: 'string',
          description: 'Field Description'
        }
      }
    }
  }
})


Replace collectionName, fieldName, and Field Description with your actual collection name, field name, and description respectively.

  1. Once the command is executed successfully, the field description will be added to the specified collection.
  2. You can verify the field description by running the following command in the mongo shell:
1
db.getCollectionInfos({name: 'collectionName'})


This will display the information about the specified collection, including the field description that was added.


That's it! You have successfully added a field description to a MongoDB collection.