{"id":2755,"date":"2023-07-20T20:46:38","date_gmt":"2023-07-20T20:46:38","guid":{"rendered":"https:\/\/statorials.org\/it\/sottostringa-mongodb\/"},"modified":"2023-07-20T20:46:38","modified_gmt":"2023-07-20T20:46:38","slug":"sottostringa-mongodb","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/sottostringa-mongodb\/","title":{"rendered":"Mongodb: come utilizzare la funzione $susbtr"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Puoi utilizzare la funzione <strong>$substr<\/strong> in MongoDB per estrarre una sottostringa da una stringa.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questa funzione utilizza la seguente sintassi di base:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>db.myCollection.aggregate([\n  { <span style=\"color: #3366ff;\">$project<\/span> : {substring: { <span style=\"color: #3366ff;\">$substr<\/span> : [ \" <span style=\"color: #ff0000;\">$fullstring<\/span> \", 0, 4 ] }}}\n])<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo particolare esempio estrae tutti e quattro i caratteri dal campo etichettato &#8220;fullString&#8221; a partire dalla posizione 0.<\/span><\/p>\n<p> <span style=\"color: #000000;\">L&#8217;esempio seguente mostra come utilizzare nella pratica questa sintassi con una <span style=\"padding: 1px; border: 1px solid black;\">vendita<\/span> in colletta con i seguenti documenti:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>db.sales.insertOne({yearMonth: 201702, amount: <span style=\"color: #008000;\">40<\/span> })\ndb.sales.insertOne({yearMonth: 201802, amount: <span style=\"color: #008000;\">32<\/span> })\ndb.sales.insertOne({yearMonth: 201806, amount: <span style=\"color: #008000;\">19<\/span> })\ndb.sales.insertOne({yearMonth: 201910, amount: <span style=\"color: #008000;\">29<\/span> })\ndb.sales.insertOne({yearMonth: 201907, amount: <span style=\"color: #008000;\">35<\/span> })<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Esempio: come utilizzare la funzione $susbtr in MongoDB<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare il codice seguente per estrarre i primi quattro caratteri dal campo &#8220;yearMonth&#8221; e visualizzarli in un nuovo campo chiamato &#8220;year&#8221;:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>db.sales.aggregate([\n  { <span style=\"color: #3366ff;\">$project<\/span> : {year: { <span style=\"color: #3366ff;\">$substr<\/span> : [ \" <span style=\"color: #ff0000;\">$yearMonth<\/span> \", 0, 4 ] }}}\n])\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo codice produce il seguente risultato:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>{ _id: ObjectId(\"620145544cb04b772fd7a929\"), year: '2017' }\n{ _id: ObjectId(\"620145544cb04b772fd7a92a\"), year: '2018' }\n{ _id: ObjectId(\"620145544cb04b772fd7a92b\"), year: '2018' }\n{ _id: ObjectId(\"620145544cb04b772fd7a92c\"), year: '2019' }\n{ _id: ObjectId(\"620145544cb04b772fd7a92d\"), year: '2019' }<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che i primi quattro caratteri del campo &#8220;meseAnno&#8221; di ciascun documento vengono visualizzati in un nuovo campo denominato &#8220;anno&#8221;.<\/span><\/p>\n<p> <span style=\"color: #000000;\">\u00c8 importante notare che questo codice <em>visualizza<\/em> solo la sottostringa.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Per aggiungere effettivamente un nuovo campo alla raccolta che contiene questa sottostringa, dobbiamo utilizzare la funzione <strong>$merge<\/strong> come segue:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>db.sales.aggregate([\n  { <span style=\"color: #3366ff;\">$project<\/span> : {year: { <span style=\"color: #3366ff;\">$substr<\/span> : [ \" <span style=\"color: #ff0000;\">$yearMonth<\/span> \", 0, 4 ] }}},\n  { <span style=\"color: #3366ff;\">$merge<\/span> : \"sales\" }\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(\"620145544cb04b772fd7a929\"),\n  yearMonth: 201702,\n  amount: 40,\n  year: '2017' }\n{ _id: ObjectId(\"620145544cb04b772fd7a92a\"),\n  yearMonth: 201802,\n  amount: 32,\n  year: '2018' }\n{ _id: ObjectId(\"620145544cb04b772fd7a92b\"),\n  yearMonth: 201806,\n  amount: 19,\n  year: '2018' }\n{ _id: ObjectId(\"620145544cb04b772fd7a92c\"),\n  yearMonth: 201910,\n  amount: 29,\n  year: '2019' }\n{ _id: ObjectId(\"620145544cb04b772fd7a92d\"),\n  yearMonth: 201907,\n  amount: 35,\n  year: '2019' }<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tieni presente che il nuovo campo intitolato &#8220;anno&#8221; \u00e8 stato aggiunto a ciascun documento della raccolta e visualizza i primi quattro caratteri del campo &#8220;annomese&#8221;.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : puoi trovare la documentazione completa per la funzione <strong>$substr<\/strong> <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/operator\/aggregation\/substr\/\" 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\/stringhe-di-concatenazione-mongodb\/\" target=\"_blank\" rel=\"noopener\">MongoDB: come concatenare stringhe di due campi<\/a><br \/><a href=\"https:\/\/statorials.org\/it\/stringa-di-sostituzione-mongodb\/\" target=\"_blank\" rel=\"noopener\">MongoDB: come sostituire le stringhe<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Puoi utilizzare la funzione $substr in MongoDB per estrarre una sottostringa da una stringa. Questa funzione utilizza la seguente sintassi di base: db.myCollection.aggregate([ { $project : {substring: { $substr : [ &#8221; $fullstring &#8220;, 0, 4 ] }}} ]) Questo particolare esempio estrae tutti e quattro i caratteri dal campo etichettato &#8220;fullString&#8221; a partire dalla [&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>MongoDB: Come utilizzare la funzione $susbtr - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come utilizzare la funzione substr in MongoDB per estrarre una sottostringa da una stringa, con esempi.\" \/>\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\/sottostringa-mongodb\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB: Come utilizzare la funzione $susbtr - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come utilizzare la funzione substr in MongoDB per estrarre una sottostringa da una stringa, con esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/sottostringa-mongodb\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-20T20:46:38+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\/sottostringa-mongodb\/\",\"url\":\"https:\/\/statorials.org\/it\/sottostringa-mongodb\/\",\"name\":\"MongoDB: Come utilizzare la funzione $susbtr - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-20T20:46:38+00:00\",\"dateModified\":\"2023-07-20T20:46:38+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come utilizzare la funzione substr in MongoDB per estrarre una sottostringa da una stringa, con esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/sottostringa-mongodb\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/sottostringa-mongodb\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/sottostringa-mongodb\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Mongodb: come utilizzare la funzione $susbtr\"}]},{\"@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":"MongoDB: Come utilizzare la funzione $susbtr - Statorials","description":"Questo tutorial spiega come utilizzare la funzione substr in MongoDB per estrarre una sottostringa da una stringa, con esempi.","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\/sottostringa-mongodb\/","og_locale":"it_IT","og_type":"article","og_title":"MongoDB: Come utilizzare la funzione $susbtr - Statorials","og_description":"Questo tutorial spiega come utilizzare la funzione substr in MongoDB per estrarre una sottostringa da una stringa, con esempi.","og_url":"https:\/\/statorials.org\/it\/sottostringa-mongodb\/","og_site_name":"Statorials","article_published_time":"2023-07-20T20:46:38+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\/sottostringa-mongodb\/","url":"https:\/\/statorials.org\/it\/sottostringa-mongodb\/","name":"MongoDB: Come utilizzare la funzione $susbtr - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-20T20:46:38+00:00","dateModified":"2023-07-20T20:46:38+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come utilizzare la funzione substr in MongoDB per estrarre una sottostringa da una stringa, con esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/sottostringa-mongodb\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/sottostringa-mongodb\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/sottostringa-mongodb\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Mongodb: come utilizzare la funzione $susbtr"}]},{"@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\/2755"}],"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=2755"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2755\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}