本記事では、MongoDBでの collection の作成、document の登録・削除・更新、mongoimport を使用した collection のインポートといった一連のデータ操作の方法を紹介します。MongoDB自体はChromebookに限定しませんが、本記事ではChromebookを使用しています。本記事で紹介するコマンドは一部です。本記事を読んだあと理解を深めるためにはMongoDBのマニュアルを参照してください。
目次
使用中のDBの確認とcollectionの作成
使用中のDBを確認する:db
db
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.27.07_db.png)
“test” というDBを使用していることが解ります。
DBの一覧を確認する:show dbs
show dbs
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.25.53_show_dbs.png)
“test”というDBを使用しているのにDB一覧に表示されないのはどういうこと??
と思った方もいるかも知れませんが、安心してください。
MongoDBの仕様通りです。
DBを切り替える:use
use <DB名>
# 例)testを使用する場合
use test
collectionを作成する:createCollection
db.createCollection(<collection名>)
# 例)現在使用中のDBに"test_collection"という名前のcollectionを作成する
db.createCollection("test_collection")
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.28.02_create_collection.png)
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.28.27_show_dbs_2.png)
collectionの一覧を確認する:show collections
show collections
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.29.51_show_collections.png)
collection への document の登録・更新・削除(例)
collection に document を登録する(1件):insert
db.test_collection.insert({"col_1":"data_1","col_2":"data_21"})
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.30.23_insert.png)
document を取得する(条件指定なし):find
db.test_collection.find()
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.30.48_find.png)
collection に documento を登録する(複数件):insertMany
db.test_collection.insertMany([
{"col_1":"data_1","col_2":"data_22"},
{"col_1":"data_1","col_2":"data_23"},
{"col_1":"data_1","col_2":"data_24"},
{"col_1":"data_2","col_2":"data_25"},
{"col_1":"data_2","col_2":"data_26"}
])
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.33.45_insertMany.png)
document を取得する(条件指定あり):find
db.test_collection.find({"col_1":"data_1"}).sort({"col_2":-1})
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.35.00_find_sort.png)
document を更新する:updateOne
db.test_collection.updateOne(
{"col_2":"data_24"},
{
$set: { "col_2": "data_242"},
$currentDate: { lastModified: true }
}
)
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-12.07.03_update-1.png)
findを実行して更新されたことを確認
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-12.07.33_find_after_update.png)
document を削除する(条件指定あり):remove
db.test_collection.remove({col_2:{$eq:"data_24"}})
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.48.19_remove.png)
findを実行して削除されたことを確認
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.48.33_remove_find.png)
document を削除する(全てのdocument):remove
db.test_collection.remove({})
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.49.14_remove_all.png)
find を実行して全ての document が削除されたことを確認
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.49.30_remove_all_find.png)
collection を削除する:drop
db.test_collection.drop();
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.50.07_collections_drop.png)
show collectionsを実行して collection が削除されたことを確認
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.50.25_collections_drop_show_collections.png)
データのインポート
csv ファイルを用意する
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.55.07_csv.png)
mongoimport を使用して csv ファイルをインポートする
mongoimport --db test --collection test_collection --type csv --file test.csv --headerline
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.55.37_import.png)
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.56.13_show_collection_after_import.png)
findを実行してデータが登録されたことを確認
![](https://kewton.blog/wp-content/uploads/2022/04/Screenshot-2022-04-24-11.56.45_find_after_import.png)
まとめ
本記事では、 本記事では、MongoDBでの collection の作成、document の登録・削除・更新、mongoimport を使用した collection のインポートといった一連のデータ操作の方法を紹介しました。ご参考になりましたら twitter をフォローして SNS でシェアして頂ければ幸いです。
ご参考
ちなみに今回は、 Linux系OSの動作確認環境として下記 Chromebook を使用しました。
14.0型フルHD × Core i3 × メモリ8GB を満たす数少ない端末です。
軽くて持ち運びしやすく開発に耐えうるスペックなのでおすすめです。
![]() | 価格:70,510円 |
![](https://www14.a8.net/0.gif?a8mat=3HKFDO+6AU69E+2HOM+BWGDT)
![](https://www16.a8.net/0.gif?a8mat=3N5WZY+F2DWVM+2PEO+1NIX2A)