mongodb入門。windowsにサーバとmongosh(CLIクライント)をインストール。CRUD操作をしてみる。SQL文と違って、コマンドというか関数っぽい感じなのね。
とりあえず、まず何をすればいいんだ?
windowsにmongodbサーバをインストールしてみよう。
https://www.mongodb.com/try/download/community
mongoshというコマンドラインツールを使う。
https://www.mongodb.com/docs/mongodb-shell/
mongodbのインストール先にmongoshを配置、環境変数の設定もする
C:\Program Files\MongoDB\Server\7.0\bin\mongosh.exe
mongoshというコマンドだけでローカルmongodbサーバに接続に行くっぽい(mongodb://localhost:27017/)
1 2 3 4 5 6 7 8 9 10 11 12 |
mongosh Current Mongosh Log ID: 662af818755826876b46b798 Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5 Using MongoDB: 7.0.8 Using Mongosh: 2.2.5 For mongosh info see: https://docs.mongodb.com/mongodb-shell/ ------ The server generated these startup warnings when booting 2024-04-25T17:40:24.910+09:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted ------ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# どんなデータベースがあるか?最初から3つあるっぽい。 show dbs admin 40.00 KiB config 72.00 KiB local 40.00 KiB # DB作成はuse DB名で自動生成される use myDB # コレクション(テーブル)も同じく自動生成 myDB> db.createCollection("persons") { ok: 1 } myDB> show collections persons # INSERTでレコード(ドキュメント)生成。 db.persons.insert({ name : "adam" , age : 25 }) DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite. { acknowledged: true, insertedIds: { '0': ObjectId('662afabf755826876b46b799') } } # 非推奨のinsertを使うな!というのでinsertOneを使う db.getCollection('persons').insertOne({ name: 'bell', age: 20, }) { acknowledged: true, insertedId: ObjectId('662afbb6755826876b46b79a') } # getCollection無しでも行けた。 db.persons.insertOne({ name: 'cecil', age: 15, }) { acknowledged: true, insertedId: ObjectId('662afc4b755826876b46b79b') } # 複数挿入のinsertMany db.persons.insertMany([ { name: 'adam', age: 25 }, { name: 'david', age: 35 } ]) { acknowledged: true, insertedIds: { '0': ObjectId('662afdaa755826876b46b79c'), '1': ObjectId('662afdaa755826876b46b79d') } } # bulkWriteは、異なる種類の操作(挿入、更新、削除)を一括操作できるらしい。トランザクション? db.collection.bulkWrite([ { insertOne: { document: { item: "journal", qty: 25 } } }, { updateOne: { filter: { item: "notebook" }, update: { $set: { qty: 60 } } }}, { deleteOne: { filter: { item: "paper" } }} ]); # SELECT文 myDB> db.persons.find() [ { _id: ObjectId('662afbb6755826876b46b79a'), name: 'bell', age: 20 }, { _id: ObjectId('662afc4b755826876b46b79b'), name: 'cecil', age: 15 }, { _id: ObjectId('662afdaa755826876b46b79c'), name: 'adam', age: 25 }, { _id: ObjectId('662afdaa755826876b46b79d'), name: 'david', age: 35 } ] # WHERE文 db.persons.find( {name:'adam'} ) [ { _id: ObjectId('662afdaa755826876b46b79c'), name: 'adam', age: 25 } ] myDB> db.persons.find({age:{$gte:20}}) [ { _id: ObjectId('662afbb6755826876b46b79a'), name: 'bell', age: 20 }, { _id: ObjectId('662afdaa755826876b46b79c'), name: 'adam', age: 25 }, { _id: ObjectId('662afdaa755826876b46b79d'), name: 'david', age: 35 } ] myDB> db.persons.find({age:{$gt:20}}) [ { _id: ObjectId('662afdaa755826876b46b79c'), name: 'adam', age: 25 }, { _id: ObjectId('662afdaa755826876b46b79d'), name: 'david', age: 35 } ] # OR条件 myDB> db.persons.find({$or:[ {age:20}, {age:35} ]}) [ { _id: ObjectId('662afbb6755826876b46b79a'), name: 'bell', age: 20 }, { _id: ObjectId('662afdaa755826876b46b79d'), name: 'david', age: 35 } ] # SELECT name FROM persons db.persons.find({}, {name:1}) [ { _id: ObjectId('662afbb6755826876b46b79a'), name: 'bell' }, { _id: ObjectId('662afc4b755826876b46b79b'), name: 'cecil' }, { _id: ObjectId('662afdaa755826876b46b79c'), name: 'adam' }, { _id: ObjectId('662afdaa755826876b46b79d'), name: 'david' } ] # ORDER BY 昇順なら1 降順なら-1 db.persons.find().sort({ age: 1 }) [ { _id: ObjectId('662afc4b755826876b46b79b'), name: 'cecil', age: 15 }, { _id: ObjectId('662afbb6755826876b46b79a'), name: 'bell', age: 20 }, { _id: ObjectId('662afdaa755826876b46b79c'), name: 'adam', age: 25 }, { _id: ObjectId('662afdaa755826876b46b79d'), name: 'david', age: 35 } ] # 急にsexフィールドを追加 db.persons.insertOne({ name: 'edy', age: 55, sex: 'male' }) { acknowledged: true, insertedId: ObjectId('662b0299755826876b46b79f') } db.persons.find().sort({ age: 1 }) [ { _id: ObjectId('662afc4b755826876b46b79b'), name: 'cecil', age: 15 }, { _id: ObjectId('662afbb6755826876b46b79a'), name: 'bell', age: 20 }, { _id: ObjectId('662afdaa755826876b46b79c'), name: 'adam', age: 25 }, { _id: ObjectId('662afdaa755826876b46b79d'), name: 'david', age: 35 }, { _id: ObjectId('662b0299755826876b46b79f'), name: 'edy', age: 55, sex: 'male' } ] # 特定フィールドが存在するドキュメントだけ抽出。これはmysqlに無いね! db.persons.find({ sex:{$exists:true} }, {_id:0}) [ { name: 'edy', age: 55, sex: 'male' } ] # count(*) myDB> db.persons.countDocuments() 4 # UPDATE文。最初がWHERE条件で、第二引数がSET db.persons.updateOne( { name: "adam" }, { $set: { age: 30 } } ) { acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 1, upsertedCount: 0 } # DELETE文 db.persons.deleteOne({ name: "adam" }) { acknowledged: true, deletedCount: 1 } # drop table db.persons.drop() |