Difference between revisions of "MongoDB"
From Wasya Wiki
(→In mongosh (old mongo):) |
|||
| (19 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| − | == | + | |
| + | = Install = | ||
| + | From: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/ | ||
| + | |||
From: https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ | From: https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ | ||
| − | + | From: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ | |
| − | + | ||
| − | + | ||
| − | + | ||
| + | = Develop = | ||
| + | |||
| + | == rename a field, or paste one field to another collection-wide == | ||
| + | |||
| + | Methods are: updateOne, updateMany or update | ||
| + | <pre> | ||
| + | db.collection.<update method>( | ||
| + | {}, | ||
| + | [ | ||
| + | {"$set": {"name": { "$concat": ["$firstName", " ", "$lastName"]}}} | ||
| + | ] | ||
| + | ) | ||
| + | </pre> | ||
== Count Documents == | == Count Documents == | ||
| Line 16: | Line 32: | ||
</pre> | </pre> | ||
| − | == Performance Tuning | + | == list users == |
| + | use admin; | ||
| + | db.getUsers(); | ||
| + | |||
| + | == create user == | ||
| + | use admin; | ||
| + | <pre> | ||
| + | db.createUser( | ||
| + | { | ||
| + | user: "myTester", | ||
| + | pwd: passwordPrompt(), // or cleartext password | ||
| + | roles: [ { role: "readWrite", db: "test" }, | ||
| + | { role: "read", db: "reporting" } ] | ||
| + | } | ||
| + | ) | ||
| + | </pre> | ||
| + | |||
| + | == add role to a user == | ||
| + | <pre> | ||
| + | |||
| + | db.grantRolesToUser('<username>', [ | ||
| + | { role: "readWrite", db: "<dbname>" } | ||
| + | ], { w: "majority" , wtimeout: 4000 }) | ||
| + | |||
| + | </pre> | ||
| + | |||
| + | = Performance Tuning = | ||
db.getProfilingStatus() | db.getProfilingStatus() | ||
| Line 22: | Line 64: | ||
db.setProfilingLevel(1, 10) | db.setProfilingLevel(1, 10) | ||
db.getCollection('system.profile').find({}) | db.getCollection('system.profile').find({}) | ||
| + | |||
| + | == profiling == | ||
| + | Profiling. From: https://docs.mongodb.com/manual/reference/method/db.setProfilingLevel/ | ||
| + | |||
| + | The profiler writes all the data it collects to the system.profile collection, a capped collection in the admin database. | ||
| + | |||
| + | db.getProfilingStatus() | ||
| + | # db.setProfilingLevel(level, options) | ||
| + | db.setProfilingLevel(1, { "slowms": 500 }) | ||
| + | |||
| + | db.enableFreeMonitoring() | ||
| + | |||
| + | use creek_development | ||
| + | db.setProfilingLevel(1, { "slowms": 500 }) | ||
| + | db.getCollection('system.profile').find({}).sort({"ts": -1}).limit( 5 ) | ||
| + | |||
| + | db.lineitems.getIndexes() | ||
| + | |||
| + | == view slow queries == | ||
| + | |||
| + | === In ruby: === | ||
| + | |||
| + | config.mongoid.logger = Logger.new(Rails.root.join( 'tmp', 'mongo.log' )) | ||
| + | config.mongoid.logger.level = Logger::DEBUG | ||
| + | |||
| + | Mongoid logging can also show caller location if you enable it: | ||
| + | |||
| + | Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::COMMAND) do |event| | ||
| + | Rails.logger.debug("Mongo Query: #{event.command}") | ||
| + | end | ||
| + | |||
| + | Use .explain on Suspicious Queries: | ||
| + | |||
| + | User.where(email: "test@example.com").explain | ||
| + | |||
| + | === In mongosh (old mongo): === | ||
| + | |||
| + | (no need to restart mongo): | ||
| + | |||
| + | use creek_staging ; | ||
| + | db.setProfilingLevel(1, { slowms: 100 }) | ||
| + | db.system.profile.find().sort({ ts: -1 }).limit(5) | ||
| + | |||
| + | This means: | ||
| + | |||
| + | * 0 → profiling off | ||
| + | * 1 → log slow queries only (recommended) | ||
| + | * 2 → log all queries (very heavy, avoid in production) | ||
Latest revision as of 19:02, 16 March 2026
Contents
Install
From: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
From: https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
From: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
Develop
rename a field, or paste one field to another collection-wide
Methods are: updateOne, updateMany or update
db.collection.<update method>(
{},
[
{"$set": {"name": { "$concat": ["$firstName", " ", "$lastName"]}}}
]
)
Count Documents
db.collection.aggregate(
[
{ $group: { _id: null, count: { $sum: 1 } } }
]
)
list users
use admin; db.getUsers();
create user
use admin;
db.createUser(
{
user: "myTester",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "readWrite", db: "test" },
{ role: "read", db: "reporting" } ]
}
)
add role to a user
db.grantRolesToUser('<username>', [
{ role: "readWrite", db: "<dbname>" }
], { w: "majority" , wtimeout: 4000 })
Performance Tuning
db.getProfilingStatus()
db.getProfilingLevel()
db.setProfilingLevel(1, 10)
db.getCollection('system.profile').find({})
profiling
Profiling. From: https://docs.mongodb.com/manual/reference/method/db.setProfilingLevel/
The profiler writes all the data it collects to the system.profile collection, a capped collection in the admin database.
db.getProfilingStatus()
# db.setProfilingLevel(level, options)
db.setProfilingLevel(1, { "slowms": 500 })
db.enableFreeMonitoring()
use creek_development
db.setProfilingLevel(1, { "slowms": 500 })
db.getCollection('system.profile').find({}).sort({"ts": -1}).limit( 5 )
db.lineitems.getIndexes()
view slow queries
In ruby:
config.mongoid.logger = Logger.new(Rails.root.join( 'tmp', 'mongo.log' )) config.mongoid.logger.level = Logger::DEBUG
Mongoid logging can also show caller location if you enable it:
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::COMMAND) do |event|
Rails.logger.debug("Mongo Query: #{event.command}")
end
Use .explain on Suspicious Queries:
User.where(email: "test@example.com").explain
In mongosh (old mongo):
(no need to restart mongo):
use creek_staging ;
db.setProfilingLevel(1, { slowms: 100 })
db.system.profile.find().sort({ ts: -1 }).limit(5)
This means:
- 0 → profiling off
- 1 → log slow queries only (recommended)
- 2 → log all queries (very heavy, avoid in production)