DBにmongoDBを使っている、mongo shellを利用することも多いと思います。
業務でよく利用するコマンドのtipsを紹介します。
検索
抽出結果にくっつけて書けば、欲しい情報を絞り込んで調査に役立つ
ああdb.HogeHoge.find({ postCount: { $gt: 1}}).sort({ postCount: -1}).limit(5)
重複レコードを検索
db.HogeHoge.aggregate([ { $sortByCount: "$uniqueKey" }, { $match: { count: {$gt: 1} } } ]);
更新
デフォルト値を設定更新
db.HogeHoge.updateMany({ "postCount": { $exists: false } }, { $set: { "postCount": 0} }, {upsert: false, multi:true})
テーブル内のデータを使って、同一レコード更新
db.HogeHoge.updateMany({ action: "admin" }, [{ $set: { uniqueKey: { $concat: [{ $replaceOne: { input: "$user", find: "user$", replacement: "" } }, "-", "$action", "-","$_id"] } }}]); 検索結果で、他のテーブルを更新 db.HogeHoge.find({ xxxxx }).forEach(doc => { db.HogeHoge2.updateOne( { _id: doc.hoge2Id }, { $set: { comment: doc.comment} } ); } );
削除
全レコード削除
db.HogeHoge.deleteMany({})