MongoDB データベースに「写真」と「アルバム」の 2 つのコレクションがあります。
アルバム コレクション内の各ドキュメントには、写真コレクションへのキー バックである ID の配列を含む「images」プロパティが含まれています。
Node.js ドライバーを使用して、アルバム コレクションを繰り返し処理し、孤立した画像 (つまり、どのアルバムにも含まれていない画像) を削除する必要があります。
私はそれを理解できないようです...これは私が書いたコードです
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/photosharing', function(err, db) {
if(err) throw err;
var images = db.collection('images');
var albums = db.collection('albums');
var myArray = [];
db.albums.count({}, function(err, count) {
if(err) throw err;
console.dir("start length of albums is " + count);
});
images.find({}).toArray(function(err, docs) {
if(err) throw err;
for (var i=1; i<=docs.length; i++) {
albums.count({"images": i}, function(err, count) {
if(err) throw err;
if ( count == 0 ) {
images.remove({images.find({ "_id": i })})
}
});
};
});
db.albums.count({}, function(err, count) {
if(err) throw err;
console.dir("end length of albums is " + count);
});
db.close();
});