MongoDB Atlas で無料の Shared Cluster を使ってみた

Free Tier であっても月に何時間までとか稼働時間に制限があったりします。ずーっと稼働し続けられる無料の DB ってなかなか無い中、NoSQL の MongoDB は同時接続数やストレージ容量に制限はあるものの、それ以外は無料で使えるようなので試してみました。

MongoDB Atlas の料金表

MongoDB Atlas の管理画面

MongoDB Cluster の管理ツール

$ brew install mongodb-atlas

$ atlas --version

atlascli version: 1.26.0
git version: homebrew-release
Go version: go1.22.5
   os: darwin
   arch: arm64
   compiler: gc

$ atlas auth register

reate and verify your MongoDB Atlas account from the web browser and return to Atlas CLI after activation.

To verify your account, copy your one-time verification code:
DT7J-KMK4

Paste the code in the browser when prompted to activate your Atlas CLI. Your code will expire after 10 minutes.

To continue, go to https://account.mongodb.com/account/register/cli
Successfully logged in as <email@address>.

$ atlas cluster list

ID                         NAME           MDB VER   STATE
66c9653f237c5f09400fd9c2   AtlasCluster   7.0.12    IDLE

mongosh で MongoDB の CRUD

$ brew install mongosh

$ mongosh --version
2.3.0

$ mongosh "mongodb+srv://atlascluster.oky1u.mongodb.net/" --apiVersion 1 --username <username>

Enter password: ********
Current Mongosh Log ID: 66c9a9a0ae6590f9ddc1c1a9
Connecting to:          mongodb+srv://<credentials>@atlascluster.oky1u.mongodb.net/?appName=mongosh+2.3.0
Using MongoDB:          7.0.12 (API Version 1)
Using Mongosh:          2.3.0

For mongosh info see: https://www.mongodb.com/docs/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.

Atlas atlas-hgarka-shard-0 [primary] test> 
db.movies.insertOne(
  {
    title: "The Favourite",
    genres: [ "Drama", "History" ],
    runtime: 121,
    rated: "R",
    year: 2018,
    directors: [ "Yorgos Lanthimos" ],
    cast: [ "Olivia Colman", "Emma Stone", "Rachel Weisz" ],
    type: "movie"
  }
)

db.movies.find( { title: "The Favourite" } )

db.movies.find( {} )

db.movies.find()

db.movies.find( { rated: { $in: [ "PG", "R" ] } } )

db.movies.updateOne( { title: "The Favourite" },
{
  $set: {
    plot: "A teenage girl risks everything–including her life–when she falls in love with a vampire."
  },
  $currentDate: { lastUpdated: true }
})

db.movies.find()

db.movies.deleteMany( { title: "The Favourite" } )

db.movies.find()

exit

参考

https://www.mongodb.com/docs/atlas/getting-started

https://www.mongodb.com/docs/mongodb-shell

タグ: