$ docker run --name mongodb-container -v ${PWD}/data:/data/db -d -p 27017:27017 mongo:6.0.5
$ docker exec -it mongodb-container bash
...
mongo:
container_name: local-mongo
image: mongo:6.0.5
restart: always
ports:
- "27017:27017"
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=1q2w3e4r
- MONGO_INITDB_DATABASE=chat
volumes:
- ./mongodb:/data/db
...
$ docker exec -it local-mongo /bin/bash
root@309a633cde79:/# mongosh -u root -p 1q2w3e4r
Current Mongosh Log ID: 642f9f52e3054971915bfee6
Connecting to: mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.0
Using MongoDB: 6.0.5
Using Mongosh: 1.8.0
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting
2023-04-07T04:41:49.024+00:00: vm.max_map_count is too low
------
------
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
------
test>
test> use mydb
switched to db mydb
mydb> show dbs
admin 100.00 KiB
config 12.00 KiB
local 72.00 KiB
mydb> db.createCollection('book')
{ ok: 1 }
mydb> db.book.insertOne({name:"hello mongo", author:"choi"})
{
acknowledged: true,
insertedId: ObjectId("642f9fb33bc0f9858df2c0b9")
}
mydb> db.book.insertMany([{name:"hello java", author:"kim"}, {name:"hello docker", author:"lee"}])
{
acknowledged: true,
insertedIds: {
'0': ObjectId("642f9fba3bc0f9858df2c0ba"),
'1': ObjectId("642f9fba3bc0f9858df2c0bb")
}
}
mydb> db.book.find().pretty()
[
{
_id: ObjectId("642f9fb33bc0f9858df2c0b9"),
name: 'hello mongo',
author: 'choi'
},
{
_id: ObjectId("642f9fba3bc0f9858df2c0ba"),
name: 'hello java',
author: 'kim'
},
{
_id: ObjectId("642f9fba3bc0f9858df2c0bb"),
name: 'hello docker',
author: 'lee'
}
]
mydb> db.book.updateOne( { _id: ObjectId("61e374779cbbcefe0d6d744d") }, { $set: { author: "lee docker" } } )
{
acknowledged: true,
insertedId: null,
matchedCount: 0,
modifiedCount: 0,
upsertedCount: 0
}
mydb> db.book.find({name:"hello docker"})
[
{
_id: ObjectId("642f9fba3bc0f9858df2c0bb"),
name: 'hello docker',
author: 'lee'
}
]
mydb> db.book.deleteOne({name:"hello docker"})
{ acknowledged: true, deletedCount: 1 }
mydb> db.book.find()
[
{
_id: ObjectId("642f9fb33bc0f9858df2c0b9"),
name: 'hello mongo',
author: 'choi'
},
{
_id: ObjectId("642f9fba3bc0f9858df2c0ba"),
name: 'hello java',
author: 'kim'
}
]
mydb>
DataGrip에서 다음과 같이 연결이 되지 않는다면,
?authSource=admin
를 추가한다.
(https://velog.io/@ymh92730/MongoDB-%EC%97%B0%EA%B2%B0-%EC%8B%A4%ED%8C%A8%EC%8B%9C)