{"id":2753,"date":"2023-07-20T20:58:17","date_gmt":"2023-07-20T20:58:17","guid":{"rendered":"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/"},"modified":"2023-07-20T20:58:17","modified_gmt":"2023-07-20T20:58:17","slug":"stringa-di-sostituzione-mongodb","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/","title":{"rendered":"Come sostituire le stringhe in mongodb (con esempio)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Puoi utilizzare la seguente sintassi per sostituire una stringa specifica in un campo in MongoDB:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>db.myCollection.updateMany(\n  { fieldName: { <span style=\"color: #3366ff;\">$regex<\/span> : \/old\/ } },\n  [{\n    <span style=\"color: #3366ff;\">$set<\/span> : { fieldName: {\n      <span style=\"color: #3366ff;\">$replaceOne<\/span> : { input: \" <span style=\"color: #ff0000;\">$fieldName<\/span> \", find: \" <span style=\"color: #ff0000;\">old<\/span> \", replacement: \" <span style=\"color: #ff0000;\">new<\/span> \" }\n    }}\n  }]\n)\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo particolare esempio sostituisce la stringa &#8220;old&#8221; con &#8220;new&#8221; nel campo denominato &#8220;fieldName&#8221; all&#8217;interno della raccolta denominata <span style=\"padding: 1px; border: 1px solid black;\">myCollection<\/span> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">L&#8217;esempio seguente mostra come utilizzare nella pratica questa sintassi con un<\/span> <span style=\"padding: 1px; border: 1px solid black;\">team<\/span> di recupero crediti <span style=\"color: #000000;\">con i seguenti documenti:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>db.teams.insertOne({team: \" <span style=\"color: #008000;\">Mavs<\/span> \", conference: \" <span style=\"color: #008000;\">Western<\/span> \", points: 31})\ndb.teams.insertOne({team: \" <span style=\"color: #008000;\">Spurs<\/span> \", conference: \" <span style=\"color: #008000;\">Western<\/span> \", points: 22})\ndb.teams.insertOne({team: \" <span style=\"color: #008000;\">Rockets<\/span> \", conference: \" <span style=\"color: #008000;\">Western<\/span> \", points: 19})\ndb.teams.insertOne({team: \" <span style=\"color: #008000;\">Celtics<\/span> \", conference: \" <span style=\"color: #008000;\">Eastern<\/span> \", points: 26})\ndb.teams.insertOne({team: \" <span style=\"color: #008000;\">Cavs<\/span> \", conference: \" <span style=\"color: #008000;\">Eastern<\/span> \", points: 33})\ndb.teams.insertOne({team: \" <span style=\"color: #008000;\">Nets<\/span> \", conference: \" <span style=\"color: #008000;\">Eastern<\/span> \", points: 38})<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Esempio: sostituisci una stringa in MongoDB<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare il seguente codice per sostituire la stringa &#8220;Western&#8221; con &#8220;West&#8221; nel campo <strong>conference<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>db.teams.updateMany(\n  { conference: { <span style=\"color: #3366ff;\">$regex<\/span> : \/Western\/ } },\n  [{\n    <span style=\"color: #3366ff;\">$set<\/span> : { conference: {\n      <span style=\"color: #3366ff;\">$replaceOne<\/span> : { input: \" <span style=\"color: #ff0000;\">$conference<\/span> \", find: \" <span style=\"color: #ff0000;\">Western<\/span> \", replacement: \" <span style=\"color: #ff0000;\">West<\/span> \" }\n    }}\n  }]\n)<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Ecco come appare ora la raccolta aggiornata:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>{ _id: ObjectId(\"620139494cb04b772fd7a8fa\"),\n  team: ' <span style=\"color: #008000;\">Mavs<\/span> ',\n  conference: ' <span style=\"color: #008000;\">West<\/span> ',\n  points: 31 }\n{ _id: ObjectId(\"620139494cb04b772fd7a8fb\"),\n  team: ' <span style=\"color: #008000;\">Spurs<\/span> ',\n  conference: ' <span style=\"color: #008000;\">West<\/span> ',\n  points: 22 }\n{ _id: ObjectId(\"620139494cb04b772fd7a8fc\"),\n  team: ' <span style=\"color: #008000;\">Rockets<\/span> ',\n  conference: ' <span style=\"color: #008000;\">West<\/span> ',\n  points: 19 }\n{ _id: ObjectId(\"620139494cb04b772fd7a8fd\"),\n  team: ' <span style=\"color: #008000;\">Celtics<\/span> ',\n  conference: ' <span style=\"color: #008000;\">Eastern<\/span> ',\n  points: 26 }\n{ _id: ObjectId(\"620139494cb04b772fd7a8fe\"),\n  team: ' <span style=\"color: #008000;\">Cavs<\/span> ',\n  conference: ' <span style=\"color: #008000;\">Eastern<\/span> ',\n  points: 33 }\n{ _id: ObjectId(\"620139494cb04b772fd7a8ff\"),\n  team: ' <span style=\"color: #008000;\">Nets<\/span> ',\n  conference: ' <span style=\"color: #008000;\">Eastern<\/span> ',\n  points: 38 }<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che ogni documento che conteneva la stringa &#8220;Ovest&#8221; nel campo <strong>conferenza<\/strong> ora ha &#8220;Ovest&#8221; nel campo <strong>conferenza<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Qualsiasi documento che non avesse la stringa &#8220;Occidentale&#8221; nel campo <strong>conferenza<\/strong> conservava semplicemente la stringa originale.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : puoi trovare la documentazione completa per la funzione <strong>$replaceOne<\/strong> <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/method\/db.collection.replaceOne\/\" target=\"_blank\" rel=\"noopener\">qui<\/a> .<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come eseguire altre operazioni comuni in MongoDB:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/mongodb-contiene\/\" target=\"_blank\" rel=\"noopener\">MongoDB: come verificare se il campo contiene una stringa<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/mongodb-aggiungi-campo\/\" target=\"_blank\" rel=\"noopener\">MongoDB: come aggiungere un nuovo campo<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/campo-di-eliminazione-mongodb\/\" target=\"_blank\" rel=\"noopener\">MongoDB: come eliminare un campo<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Puoi utilizzare la seguente sintassi per sostituire una stringa specifica in un campo in MongoDB: db.myCollection.updateMany( { fieldName: { $regex : \/old\/ } }, [{ $set : { fieldName: { $replaceOne : { input: &#8221; $fieldName &#8220;, find: &#8221; old &#8220;, replacement: &#8221; new &#8221; } }} }] ) Questo particolare esempio sostituisce la stringa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Come sostituire le stringhe in MongoDB (con esempio) \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come sostituire le stringhe in MongoDB, con un esempio.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come sostituire le stringhe in MongoDB (con esempio) \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come sostituire le stringhe in MongoDB, con un esempio.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-20T20:58:17+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/\",\"url\":\"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/\",\"name\":\"Come sostituire le stringhe in MongoDB (con esempio) \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-20T20:58:17+00:00\",\"dateModified\":\"2023-07-20T20:58:17+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come sostituire le stringhe in MongoDB, con un esempio.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come sostituire le stringhe in mongodb (con esempio)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come sostituire le stringhe in MongoDB (con esempio) \u2013 Statorials","description":"Questo tutorial spiega come sostituire le stringhe in MongoDB, con un esempio.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/","og_locale":"it_IT","og_type":"article","og_title":"Come sostituire le stringhe in MongoDB (con esempio) \u2013 Statorials","og_description":"Questo tutorial spiega come sostituire le stringhe in MongoDB, con un esempio.","og_url":"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/","og_site_name":"Statorials","article_published_time":"2023-07-20T20:58:17+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"2 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/","url":"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/","name":"Come sostituire le stringhe in MongoDB (con esempio) \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-20T20:58:17+00:00","dateModified":"2023-07-20T20:58:17+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come sostituire le stringhe in MongoDB, con un esempio.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come sostituire le stringhe in mongodb (con esempio)"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2753"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=2753"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2753\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}