To find first word of a sentence in mongodb of specified size

755 views Asked by At

I want to find the first word from a sentence(phrase) whose size is less than 3 letters.Is there any way i could find it? please suggest.

I have used

 .map(function(st){return st.split(" ")[0];} 

function it gives me all the first words in array format.But this is not the expected output.

{ "name" : "VAS LAYER BREED FARM PRIVATE LIMITED" }

{ "name" : "UTTARA BROILER BREED FARM PRIVATE LTD" }

{ "name" : "SAI REKHA POULTRY PRIVATE LTD" }

{ "name" : "RUHITECH NUTRITION PRIVATE LTD" }

{ "name" : "SADKAR BROILER AND AGRO FARMS PRIVATE LTD" }

{ "name" : "SADAR POULTRY PRIVATE LTD" }

From this list i need the output to print only the words: ("SAI","VAS") in the output.

4

There are 4 answers

1
Valijon On BEST ANSWER

You may perform aggregation query.

db.collection.aggregate([
  {
    $project: {
      name: {
        $let: {
          vars: {
            first: {
              $arrayElemAt: [
                {
                  $split: [
                    "$name",
                    " "
                  ]
                },
                0
              ]
            }
          },
          in: {
            $cond: [
              {
                $lte: [
                  {
                    $strLenCP: "$$first"
                  },
                  3
                ]
              },
              "$$first",
              ""
            ]
          }
        }
      }
    }
  },
  {
    $match: {
      name: {
        $ne: ""
      }
    }
  },
  {
    $group: {
      _id: null,
      name: {
        $push: "$name"
      }
    }
  }
])

MongoPlayground

0
prasad_ On

The following mongo shell query will print all the words which are less than three characters.

db.test.find().forEach( doc => {
    let words = doc.name.split(" ");
    for ( let word of words ) {
        if ( word.length < 3 ) {
            print( word );
            break;
        }
    }
} )

The query will print the character "&".

If you want the query to print words less than or equal to three characters, change the code word.length < 3 to word.length <= 3. This will print "&","SAI" and "VAS".

If you want the query print only words with alphabets (A to Z and a to z), change ( word.length < 3 ) to ( word.length <= 3 && word.match("^[A-Za-z]+$") ). This will print "SAI" and "VAS".

1
Subburaj On

By what I understood from your question may be you are looking for this:

INPUT

var arr = [{ "name" : "VAS LAYER BREEDER FARM PRIVATE LIMITED" },
{ "name" : "UTTARA BROILER BREEDER FARM PRIVATE LIMITED" },
{ "name" : "SAI REKHA POULTRY FARMS PRIVATE LIMITED" }
]

CODE

var outputArr = [];
for(let j=0; j<arr.length; j++){
    var chars = arr[j].name.split(' ');    
    for(let i=0; i<chars.length; i++){
        if(chars[i].length <= 3){
            outputArr.push(chars[i]);
            break;    
        }
    };
}

OUTPUT

outputArr [ 'VAS', 'SAI' ]
1
Sarfraaz On

If you want to manage the same with javascript (as your sample code suggests)

// Your question suggests you need words less than 3 length, so you can write
.map((st)=> st.split(" ")[0]).filter((str)=> str.length < 3)

// Your expected output suggests you need words less than or equal to 3 length, so you can write
.map((st)=> st.split(" ")[0]).filter((str)=> str.length <= 3)