The default port for mongod and mongos instances. You can change this port with port or --port.
27018
The default port for mongod when running with --shardsvr command-line option or the shardsvr value for the clusterRole setting in a configuration file.
27019
The default port for mongod when running with --configsvr command-line option or the configsvr value for the clusterRole setting in a configuration file.
//
// Create a new user
//
mongo admin --host localhost:27000 --eval'
db.createUser({
user: "m103-admin",
pwd: "m103-pass",
roles: [
{role: "root", db: "admin"}
]
})
'
// OR
use admin
db.createUser({
user: "myUserAdmin",
pwd: passwordPrompt(), // or cleartext password
roles: [{ role: "userAdminAnyDatabase", db: "admin"}, "readWriteAnyDatabase"]})
//
// Grant role to db user
//
db.grantRolesToUser("dba", [{ db: "playground", role: "dbOwner"}])
//
// Query the role privileges
//
db.runCommand({ rolesInfo: { role: "dbOwner", db: "playground"}, showPrivileges: true})
MongoDB Shell (Common operations)
# Connect to MongoDB server
mongo "mongodb+srv://cluster0.rwrtm.mongodb.net/<dbname>"--usernametest
// OR
mongo --username root --password root123 --authenticationDatabase admin
# Show all existing databases
show dbs
# or
show databases
# Switch database
use <database_name>
# Show all collection within current database
show collections
# Create a new collection
db.createCollection('<collection_name>');# Create index
db.<collection>.createIndex({"product": 1 },
{"name": "name_index"})# Load data into database
load('loadMovieDetailsDataset.js')# Shutdown the Mongod (*Must switch to 'admin' database first)
db.shutdownServer()
// OR
db.adminCommand({ shutdown: 1 })# Iterate next result set in cursor
it
# Select document## Select all data with pretty print out
db.<collection_name>.find().pretty()## Select with matching criteria for array or nested object
db.movieDetails.find({"genres.1": "Western"})## Projection: Select the 'genres' field in return only (1: include, 0: exclude)
db.movieDetails.find({"year": 1982 }, {"genres": 1 })## With comparison operators ($eq, $gt, $gte, $in, $lt, $lte, $ne, $nin)
db.movieDetails.find({ writers: {$in: ['Ethan Coen', 'Joel Coen']}})## With element operators ($exists, $type)
db.movies.find({ viewerRating: {$type: 'int'}})## With logical operators ($and, $not, $nor, $or)
db.shipwracks.find({$or: [{watlev: {$eq: 'always dry'}}, {$and: [{depth: {$type: 'int'}}, {depth: {$eq: 0}}]}]})## With array operator ($all, $elemMatch, $size)
db.movieDetails({genres: {$all: ["Comedy", "Crime", "Drama"]}})## WIth evaluation operators ($expr, $jsonSchema, $mod, $regex, $text, $where)
db.movieDetails({'awards.text': {$regex: /^Won .*/}})# Insert document## Insert only one document
db.<collection_name>.insertOne({});## Insert few documents at a time
db.<collection_name>.insertMany([],{"ordered": <boolean> });# Update document# Update only one document
db.moviesScratch.updateOne({"type": "movie"}, {$set: {"type": "book"}})## Update few documents at a time
db.moviesScratch.updateMany({}, {$set: {"star": ""}})## Update or Insert a new document if not existed
db.moviesScratch.updateOne({ title: "Steve Jobs"}, {$set: { title: "Steve Jobs", year: 2000, type: "movie", comment: []}}, { upsert: true})# Replace whole document (field '_id' is immutable and can't be changed)
db.moviesScratch.replaceOne({title:"Steve Jobs"}, tmp)# Delete document## Delete only one document
db.moviesScratch.deleteOne({title:"Steve Jobs 2"})## Delete few documents at a time
db.moviesScratch.deleteMany({title:"Steve Jobs"})
# Get the logging components:
db.getLogComponents()# Change the logging level:
db.setLogLevel(0, "index")# View the logs through the Mongo shell:
db.adminCommand({"getLog": "global"})
Profile related command:
# Get profiling level
db.getProfilingLevel()# Set profiling level:
db.setProfilingLevel(1)# Set slowms to 0:
db.setProfilingLevel( 1, { slowms: 0 })# Get profiling data from system.profile:
db.system.profile.find().pretty()
Top comments (0)