mongodbのコレクションから別のコレクション(または存在する場合は更新)にjavaを使用してドキュメントをコピーしたい。コレクションを別のコレクションにコピーする(存在する場合は更新する)
既存のコレクションの各フィールドを追加して別のフィールドに挿入したくないです。これどうやってするの?
ここに2つのコレクション、temp
とnational
があります。 temp
にはコレクションが1つしかありません。コレクションはnational
にコピーするか、存在する場合は更新する必要があります。
MongoCursor<Document> cursor = db.getCollection("temp").find().iterator();
try {
Document doc = new Document(cursor.next());
Document new_doc = new Document("$set",doc);
doc.append("booking_id",cursor.next().get("booking_id"));
MongoCursor<Document> cursor1 = db.getCollection("national").find(doc).iterator();
Bson filter = Filters.eq("booking_id", args);
Bson update = Filters.elemMatch("booking_id", filter);
UpdateOptions options = new UpdateOptions().upsert(true);
national.updateOne(filter, new_doc, options);
}
finally {
cursor.close();
}
あなたが存在する場合はAとBを交換する、または存在しない場合は、新しいものを作成する意味ですか? – Dada
はい、Aは "temp"コレクションの文書で、Bは "national"コレクションの文書です –