{"id":27471,"date":"2020-07-24T10:10:06","date_gmt":"2020-07-24T08:10:06","guid":{"rendered":"https:\/\/pi3g.com\/?p=27471"},"modified":"2020-07-24T10:10:06","modified_gmt":"2020-07-24T08:10:06","slug":"how-to-set-unique-object-ids-in-embedded-documents-across-a-mongodb-collection","status":"publish","type":"post","link":"https:\/\/pi3g.com\/de\/how-to-set-unique-object-ids-in-embedded-documents-across-a-mongodb-collection\/","title":{"rendered":"Festlegen eindeutiger Objekt-IDs in eingebetteten Dokumenten in einer MongoDB-Sammlung"},"content":{"rendered":"<p>MongoDBs updateMany kann verwendet werden, um alle Dokumente zu aktualisieren, die \u00fcbereinstimmen. <\/p>\n<h1>Problem<\/h1>\n<p>Wenn Sie jedoch bei der Aktualisierung eindeutige neue ObjectIDs setzen wollen, werden Sie feststellen, dass MongoDB f\u00fcr jeden Eintrag die gleiche ObjectID setzt:<\/p>\n<blockquote>\n<p>db.getCollection('mypis_collectedpis').updateMany({}, {$set: <br \/>&nbsp;&nbsp;&nbsp;&nbsp; {apps: {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \"com.picockpit\/core\": {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; meta: {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Version: \"2.0\",<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name: \"PiCockpit Core\",<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Bild: \"\",<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Beschreibung: \"PiCockpit.com Kernanwendung\"<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; },<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; id: ObjectId()<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; },<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ts: ISODate(\u201c2020-07-23T08:42:41.052Z\u201d)}<br \/>&nbsp;&nbsp;&nbsp; })<\/p>\n<p><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image-5.png\"><img loading=\"lazy\" decoding=\"async\" width=\"782\" height=\"150\" title=\"Bild\" style=\"display: inline; background-image: none;\" alt=\"Bild\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image_thumb-4.png\" border=\"0\"><\/a><\/p>\n<p>Dies liegt daran, dass die ObjectId einmal erstellt und dann f\u00fcr jede Aktualisierung verwendet wird.<\/p>\n<h1>L\u00f6sung<\/h1>\n<p>Sie m\u00fcssen mit einem Cursor \u00fcber die einzelnen Dokumente iterieren und die ObjectIDs setzen. <\/p>\n<h2>Erster Schritt<\/h2>\n<p>Aktualisieren Sie die Werte, die in allen Ihren Dokumenten gleich sein sollen.<\/p>\n<p>Ich schlage vor, die eindeutigen Werte bereits in diesem Schritt zun\u00e4chst auf Null zu setzen:<\/p>\n<blockquote>\n<p>db.getCollection('mypis_collectedpis').updateMany({}, {$set: <br \/>&nbsp;&nbsp;&nbsp;&nbsp; {apps: {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \"com.picockpit\/core\": {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; meta: {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Version: \"2.0\",<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name: \"PiCockpit Core\",<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Bild: \"\",<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Beschreibung: \"PiCockpit.com Kernanwendung\"<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; },<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; id: null<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; },<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ts: ISODate(\u201c2020-07-23T08:42:41.052Z\u201d)}<br \/>&nbsp;&nbsp;&nbsp; })<\/p>\n<p><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image-6.png\"><img loading=\"lazy\" decoding=\"async\" width=\"475\" height=\"220\" title=\"Bild\" style=\"display: inline; background-image: none;\" alt=\"Bild\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image_thumb-5.png\" border=\"0\"><\/a><\/p>\n<p>NB: Ich verwende Robo 3T, weil es bequemer ist als die MongoDB-Befehlszeilen-Shell.<\/p>\n<p>erhalten Sie:<\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image-7.png\"><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"63\" title=\"Bild\" style=\"display: inline; background-image: none;\" alt=\"Bild\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image_thumb-6.png\" border=\"0\"><\/a><\/p>\n<h2>Zweiter Schritt<\/h2>\n<p>Iterieren Sie \u00fcber die Dokumente und aktualisieren Sie jedes mit updateOne:<\/p>\n<blockquote>\n<p>db.getCollection('mypis_collectedpis').find().forEach((pi) =&gt; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp; print(\"Aktualisierung\" + pi.name + \" mit ObjectId: \" + pi._id);<br \/>&nbsp;&nbsp;&nbsp;&nbsp; db.getCollection('mypis_collectedpis').updateOne({_id: pi._id}, {$set: {\"apps.id\": ObjectId()}});<br \/>&nbsp;&nbsp;&nbsp;&nbsp; })<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image-8.png\"><img loading=\"lazy\" decoding=\"async\" width=\"733\" height=\"68\" title=\"Bild\" style=\"display: inline; background-image: none;\" alt=\"Bild\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image_thumb-7.png\" border=\"0\"><\/a><\/p>\n<p>Beachten Sie die Punktschreibweise f\u00fcr den Zugriff auf Eintr\u00e4ge in einem eingebetteten Dokument - f\u00fcr die Punktschreibweise muss der Eintragsschl\u00fcssel in Anf\u00fchrungszeichen gesetzt werden!<\/p>\n<p>Das ist \u00fcbrigens die Ausgabe, die dabei entsteht:<\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image-9.png\"><img loading=\"lazy\" decoding=\"async\" width=\"554\" height=\"210\" title=\"Bild\" style=\"display: inline; background-image: none;\" alt=\"Bild\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image_thumb-8.png\" border=\"0\"><\/a><\/p>\n<p>und das ist das Ergebnis, f\u00fcr zwei verschiedene Eintr\u00e4ge:<\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image-10.png\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"74\" title=\"Bild\" style=\"display: inline; background-image: none;\" alt=\"Bild\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image_thumb-9.png\" border=\"0\"><\/a><\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image-11.png\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"71\" title=\"Bild\" style=\"display: inline; background-image: none;\" alt=\"Bild\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2020\/07\/image_thumb-10.png\" border=\"0\"><\/a><\/p>\n<p>Die ObjectIDs wurden mit eindeutigen Werten aktualisiert.<\/p>\n<h2>Wichtiger Hinweis<\/h2>\n<p>Beachten Sie, dass es in diesem Code keine Sperren \/ keine Transaktionen gibt - damit diese L\u00f6sung funktioniert, m\u00fcssten Sie exklusiven Zugriff auf die Datenbank haben oder sicherstellen, dass die Daten nicht ge\u00e4ndert werden, w\u00e4hrend Sie an der Datenbank arbeiten.<\/p>\n<p>Andernfalls muss eine Art Aktualisierungsschema erstellt werden - eine Aufgabe, die dem Leser \u00fcberlassen bleibt.<\/p>\n<p>Vielen Dank an Maximilian Schwarzm\u00fcller f\u00fcr seinen Udemy MongoDB-Kurs und die hervorragende MongoDB-Online-Dokumentation!<\/p>","protected":false},"excerpt":{"rendered":"<p>MongoDBs updateMany kann verwendet werden, um alle Dokumente zu aktualisieren, die \u00fcbereinstimmen. Problem Wenn Sie jedoch bei Ihrer Aktualisierung eindeutige neue ObjectIDs setzen wollen, w\u00fcrden Sie feststellen, dass MongoDB f\u00fcr jeden Eintrag dieselbe ObjectID setzt: db.getCollection('mypis_collectedpis').updateMany({}, {$set:      {apps: { \"com.picockpit\/core\": { meta: { version: \"2.0\", name: \"PiCockpit Core\", image: \"\", description: \"PiCockpit.com...<\/p>","protected":false},"author":830,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[830],"tags":[832,831,839,838,836,835,531,833,834,837],"class_list":["post-27471","post","type-post","status-publish","format-standard","hentry","category-mongodb","tag-console","tag-cursor","tag-different-objectids","tag-distinct-objectids","tag-embedded-documents","tag-iterate-over-objects","tag-mongodb","tag-objectid","tag-unique-objectids","tag-updatemany"],"_links":{"self":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/27471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/users\/830"}],"replies":[{"embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/comments?post=27471"}],"version-history":[{"count":1,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/27471\/revisions"}],"predecessor-version":[{"id":27472,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/27471\/revisions\/27472"}],"wp:attachment":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/media?parent=27471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/categories?post=27471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/tags?post=27471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}