Performing string operations

ReQL provides the following functions to manipulate and search strings:

  • Match() takes a string or a regular expression as an input and performs a search over the field. If it matches, it returns the data in the cursor, which we can loop over to retrieve the actual data.
  • For example, we have to find all the users whose name starts with J. Here is the query for the same:
      rethinkdb.table("users").filter(function(user) { 
      return user("name").match("^J"); 
      }).run(connection,function(err,cursor) { 
      if(err) { 
      throw new Error(err); 
        } 
       cursor.toArray(function(err,data) { 
       console.log(data); 
        }); 
      }); 
  • Here we are first performing a filter, and inside it, we put our match() condition. The filter gives every document to the match() function and it appends it to the cursor. Upon running, you should be able to view the users with names starting with J.
  • split() takes an optional argument as a separator to filter out the string and returns an array that contains the split part of the string. If no argument is present, it is separated by a space.
  • For example, you have an address stored like this: Suite 300. If you want to split them by space, you can do so as follows(assuming the same structure of the document as we mentioned previously):
      rethinkdb.table("users").get('f6f1f0ce-32dd-4bc6-885d
      97fe07310845')("address)
      ("address1").split().run(connection,function(err,result) { 
      if(err) { 
               throw new Error(err); 
              } 
      console.log(result); 
      }); 
  • You should recieve the following output:
      [ 'suite', '300' ] 
      [ 'suite', '300' ] 
  • Make sure your first input is a string and not an object. You can also provide any separator, say|, &, *, and so on. You can even provide an optional argument that limits the number of splits.
  • The rest of the two functions will allow you to change the casing of the string. For example, if you want the name to appear in capital casing only, you can run the following query to do so:
      rethinkdb.table("users").get('f6f1f0ce-32dd-4bc6-885d
      97fe07310845'
      ("name").upcase().run(connection,function(err,result) { 
      if(err) { 
                throw new Error(err); 
              } 
      console.log(result); 
      }); 

You should be receiving the name in uppercase on the console. You can use downcase() in the same way.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.40.32