{"id":2757,"date":"2023-07-20T20:34:55","date_gmt":"2023-07-20T20:34:55","guid":{"rendered":"https:\/\/statorials.org\/it\/lunghezza-della-stringa-mongodb\/"},"modified":"2023-07-20T20:34:55","modified_gmt":"2023-07-20T20:34:55","slug":"lunghezza-della-stringa-mongodb","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/lunghezza-della-stringa-mongodb\/","title":{"rendered":"Mongodb: come trovare la lunghezza di una stringa"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Puoi utilizzare i seguenti metodi per trovare la lunghezza di una stringa in MongoDB e utilizzare quella lunghezza di stringa nelle query:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Metodo 1: Trova la lunghezza della catena<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>db.myCollection.aggregate([\n  { <span style=\"color: #3366ff;\">$project<\/span> : {\n        \"name\": 1,\n        \"length\": { <span style=\"color: #3366ff;\">$strLenCP<\/span> : \" <span style=\"color: #ff0000;\">$name<\/span> \" }\n  }}\n])\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 2: trova documenti la cui stringa \u00e8 maggiore di una certa lunghezza<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>db.myCollection.find({ \n    \"name\": { <span style=\"color: #3366ff;\">$exists<\/span> : true },\n    <span style=\"color: #3366ff;\">$expr<\/span> : { <span style=\"color: #3366ff;\">$gt<\/span> : [ { <span style=\"color: #3366ff;\">$strLenCP<\/span> : \" <span style=\"color: #ff0000;\">$name<\/span> \" }, <span style=\"color: #008000;\">14<\/span> ] } \n})\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">I seguenti esempi mostrano come utilizzare ciascun metodo con un<\/span> <span style=\"padding: 1px; border: 1px solid black;\">team<\/span> di raccolta <span style=\"color: #000000;\">con i seguenti documenti:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>db.teams.insertOne({name: \" <span style=\"color: #008000;\">Dallas Mavs<\/span> \", points: 31})\ndb.teams.insertOne({name: \" <span style=\"color: #008000;\">San Antonio Spurs<\/span> \", points: 22})\ndb.teams.insertOne({name: \" <span style=\"color: #008000;\">Houston Rockets<\/span> \", points: 19})\ndb.teams.insertOne({name: \" <span style=\"color: #008000;\">Boston Celtics<\/span> \", points: 26})\ndb.teams.insertOne({name: \" <span style=\"color: #008000;\">Cleveland Cavs<\/span> \", points: 33})<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 1: trovare la lunghezza di una stringa<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare il seguente codice per calcolare la lunghezza della stringa nel campo &#8220;nome&#8221; di ciascun documento:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>db.teams.aggregate([\n  { <span style=\"color: #3366ff;\">$project<\/span> : {\n        \"name\": 1,\n        \"length\": { <span style=\"color: #3366ff;\">$strLenCP<\/span> : \" <span style=\"color: #ff0000;\">$name<\/span> \" }\n  }}\n])<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questo codice restituisce i seguenti risultati:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>{ _id: ObjectId(\"62014eff4cb04b772fd7a93d\"),\n  name: 'Dallas Mavs',\n  length: 11 }\n{ _id: ObjectId(\"62014eff4cb04b772fd7a93e\"),\n  name: 'San Antonio Spurs',\n  length: 17 }\n{ _id: ObjectId(\"62014eff4cb04b772fd7a93f\"),\n  name: 'Houston Rockets',\n  length: 15 }\n{ _id: ObjectId(\"62014eff4cb04b772fd7a940\"),\n  name: 'Boston Celtics',\n  length: 14 }\n{ _id: ObjectId(\"62014eff4cb04b772fd7a941\"),\n  name: 'Cleveland Cavs',\n  length: 14 }<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Il valore della lunghezza mostra la lunghezza della stringa nella colonna &#8220;nome&#8221;.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Per esempio:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">La lunghezza della catena &#8220;Dallas Mavs&#8221; \u00e8 di <strong>11<\/strong> .<\/span><\/li>\n<li> <span style=\"color: #000000;\">La lunghezza della catena &#8216;San Antonio Spurs&#8217; \u00e8 <strong>17<\/strong> .<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">E cos\u00ec via.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Tieni presente che la lunghezza conta anche gli spazi vuoti.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 2: trovare documenti la cui stringa \u00e8 maggiore di una certa lunghezza<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare il seguente codice per restituire solo i documenti la cui stringa nella colonna &#8220;nome&#8221; \u00e8 maggiore di <strong>14<\/strong> :<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>db.teams.find({ \n    \"name\": { <span style=\"color: #3366ff;\">$exists<\/span> : true },\n    <span style=\"color: #3366ff;\">$expr<\/span> : { <span style=\"color: #3366ff;\">$gt<\/span> : [ { <span style=\"color: #3366ff;\">$strLenCP<\/span> : \" <span style=\"color: #ff0000;\">$name<\/span> \" }, <span style=\"color: #008000;\">14<\/span> ] } \n})<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Questa query restituisce i seguenti risultati:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>{ _id: ObjectId(\"62014eff4cb04b772fd7a93e\"),\n  name: 'San Antonio Spurs',\n  points: 22 }\n{ _id: ObjectId(\"62014eff4cb04b772fd7a93f\"),\n  name: 'Houston Rockets',\n  points: 19 }<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Tieni presente che le uniche due squadre restituite sono quelle il cui campo &#8220;nome&#8221; \u00e8 maggiore di <strong>14<\/strong> .<\/span><\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Nota<\/strong> : puoi trovare la documentazione completa per la funzione <strong>$strLenCP<\/strong> <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/operator\/aggregation\/strLenCP\/\" 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\/stringa-divisa-mongodb\/\" target=\"_blank\" rel=\"noopener\">MongoDB: come dividere una stringa in un array di sottostringhe<\/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 in<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Puoi utilizzare i seguenti metodi per trovare la lunghezza di una stringa in MongoDB e utilizzare quella lunghezza di stringa nelle query: Metodo 1: Trova la lunghezza della catena db.myCollection.aggregate([ { $project : { &#8220;name&#8221;: 1, &#8220;length&#8221;: { $strLenCP : &#8221; $name &#8221; } }} ]) Metodo 2: trova documenti la cui stringa \u00e8 maggiore [&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 trovare la lunghezza di una stringa - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come trovare la lunghezza di una stringa in MongoDB e utilizzare quella lunghezza di stringa nelle query.\" \/>\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\/lunghezza-della-stringa-mongodb\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB: Come trovare la lunghezza di una stringa - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come trovare la lunghezza di una stringa in MongoDB e utilizzare quella lunghezza di stringa nelle query.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/lunghezza-della-stringa-mongodb\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-20T20:34:55+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\/lunghezza-della-stringa-mongodb\/\",\"url\":\"https:\/\/statorials.org\/it\/lunghezza-della-stringa-mongodb\/\",\"name\":\"MongoDB: Come trovare la lunghezza di una stringa - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-20T20:34:55+00:00\",\"dateModified\":\"2023-07-20T20:34:55+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come trovare la lunghezza di una stringa in MongoDB e utilizzare quella lunghezza di stringa nelle query.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/lunghezza-della-stringa-mongodb\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/lunghezza-della-stringa-mongodb\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/lunghezza-della-stringa-mongodb\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Mongodb: come trovare la lunghezza di una stringa\"}]},{\"@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 trovare la lunghezza di una stringa - Statorials","description":"Questo tutorial spiega come trovare la lunghezza di una stringa in MongoDB e utilizzare quella lunghezza di stringa nelle query.","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\/lunghezza-della-stringa-mongodb\/","og_locale":"it_IT","og_type":"article","og_title":"MongoDB: Come trovare la lunghezza di una stringa - Statorials","og_description":"Questo tutorial spiega come trovare la lunghezza di una stringa in MongoDB e utilizzare quella lunghezza di stringa nelle query.","og_url":"https:\/\/statorials.org\/it\/lunghezza-della-stringa-mongodb\/","og_site_name":"Statorials","article_published_time":"2023-07-20T20:34:55+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\/lunghezza-della-stringa-mongodb\/","url":"https:\/\/statorials.org\/it\/lunghezza-della-stringa-mongodb\/","name":"MongoDB: Come trovare la lunghezza di una stringa - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-20T20:34:55+00:00","dateModified":"2023-07-20T20:34:55+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come trovare la lunghezza di una stringa in MongoDB e utilizzare quella lunghezza di stringa nelle query.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/lunghezza-della-stringa-mongodb\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/lunghezza-della-stringa-mongodb\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/lunghezza-della-stringa-mongodb\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Mongodb: come trovare la lunghezza di una stringa"}]},{"@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\/2757"}],"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=2757"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2757\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}