List of MongoDB Useful Query

  • To get the distinct value on the specified field
db.collection_name.distinct( "field_name" )
  • To get the specific field in the document like select column from table
db.getCollection('collection_name').find({},{"field_name" : 1})
  • To export data from MongoDb
mongoexport --host <server_ip> --db <db_name> --collection <collection_name> --out <file_name>.json
db.getCollection('collection_name').find({ field_name: { $type: 2 } })
  • To know the indexes on MongoDb collection
db.getCollection('collection_name').getIndexes()
  • To create the indexes on collection
db.getCollection('collection_name').createIndex( { field_name_1: 1, field_name_2: 1  } )
  • Multi document update query in db
db.collection_name.update(
   { field_name_1: { $lte: 10 } },
   { $set: { field_name_2: true } },
   { multi: true }
)

  • 23
    Shares

Leave a Reply

Your email address will not be published.