creating Javascript functions in a MongoDB database

Although it’s not recommended to store your application logic in MongoDB, it’s sometimes useful to create Javascript functions for database management purposes in the database.

The process is really easy. MongoDB has a special collection named system.js, that can contain Javascript functions. Creating a function in MongoDB is as simple as inserting a record with the name and the function code into the fields named _id and value in this collection.

Example:

db.system.js.save(
  {
    _id : "myFirstFunction" ,
    value : function (a, b){ return a + b; }
  }
);

The function is now accessible in any Javascript context (for instance in a $where). If you need to access the functions from the MongoDB shell, call the command db.loadServerScripts() to load them for the current database.

 

Printing large XMLTYPE values with DBMS_OUTPUT

Printing large XMLTYPE values with DBMS_OUTPUT

As a sort of primitive debugging-method I wanted to show the contents of an XMLTYPE variable using DBMS_OUTPUT.PUT_LINE.

When you want to extract the contents of a large XMLTYPE you can use the method getClobVal(). There’s another method, getStringVal(), but this will raise an exception if the contents of the XMLTYPE exceeds 32k (max size of varchar2).

So, let’s try:

dbms_output.put_line(xml_out.getclobval);

This gives:

Error at line 1
ORA-06502: PL/SQL: numeric or value error

Hm. Apparently DBMS_OUTPUT is not equipped to work with CLOBs. So we have to resort to an old-school chunking-routine. I shamelessly copied this one fromStackOverflow and came up with this code, which I’m putting here also as a reminder to myself.

declare
  xml_out xmltype;

  -- Internal procedure to print a CLOB using dbms_output in chunks
  procedure print_clob( p_clob in clob ) is
    v_offset number := 1;
    v_chunk_size number := 10000;
  begin
    loop
      exit when v_offset > dbms_lob.getlength(p_clob);
      dbms_output.put_line( dbms_lob.substr( p_clob, v_chunk_size, v_offset ) );
      v_offset := v_offset + v_chunk_size;
    end loop;
  end print_clob;
begin
  -- Equivalent to set serveroutput on size unlimited
  dbms_output.enable(null);

  MYPROC_THAT_PRODUCES_LARGE_XML(xml_out);

  print_clob(xml_out.getClobVal);
end;

Speeding up your Time Machine backups

Okay, this has to be the weirdest trick I’ve ever posted. Usually I’m a happy Mac user, but sometimes I don’t understand why things are so slow. Like an initial backup of the SSD in my MacBook Air. Size: approximately 160GB. time to complete backup: Forever.

How to speed up a Time Machine backup

When checking the activity with my favorite tool, iStat Menus,  I see that there is hardly any disk activity and the backup daemon is doing next to nothing. So, plenty of room for improvement, I’d think. The solution is as simple as it is illogical.

Try opening a terminal. Click on the little magnifier icon in the top right or press CMD+Space simultaneous, and type in “Terminal”. Then press enter.

Now copy and paste the following command in the terminal window:

sudo sysctl debug.lowpri_throttle_enabled=0

how to speed up time machine backups

Wait a minute or so, and see how the speed of your backup increases. Don’t ask me why Apple chooses to “throttle” the Time Machine. Apparently it is in a group of low priority processes, something you don’t really want when doing an initial or large backup.

You might want to set it back to default when ready, though. Just enter:

sudo sysctl debug.lowpri_throttle_enabled=1

and throttling is enabled again.

how to speed up time machine backups 

[Dutch] Meld je aan voor Eye on IT

The Human Network

Eye on IT – inspirerende keynotes en presentaties

Op 16 mei 2017 vindt het Eye on IT evenement plaats bij The Human Network. In een wereld waarin digitalisering zorgt voor snelle ontwikkelingen, is het belangrijk op de hoogte te blijven van die ontwikkelingen. Kom naar dit evenement en laat u inspireren door onze keynote-sprekers die u alles vertellen over de ontwikkelingen op het gebied van privacywetgeving en kunstmatige intelligentie. Daarnaast zijn er verschillende breakoutsessies, waaronder de volgende onderwerpen:

  • Secure and manage your digital transformation
  • Service-oriëntatie voor applicatieontwikkeling: van monoliet naar microservices (presentatie door ondergetekende)
  • Test automatisering hype of trend?

Aanmelden kan via het formulier onderaan deze pagina.

Converting a hexstring to ObjectId for MongoDB in Javascript

How to convert a string value to ObjectID

Sometimes, the ObjectID of your MongoDB document is delivered as a 24 character hexstring. You can’t just compare this hexstring to an ObjectID, because the comparison will fail.

The solution is as follows:

var mongoose = require('mongoose' );
var hexstring = '58666c89d4bc6622ed5373dc';
// convert it to ObjectID
var ObjIDtype = mongoose.Types.ObjectId(hexstring);

The variable ObjIDtype now contains the value as type ObjectID and can be compared to MongoDB objectID’s.

How to fix SVN not able to update or clean working copy

Past week, it happened twice to me that SVN was not able to update. When updating or committing, the message “Previous operation has not finished; run ‘cleanup’ if it was interrupted” appears. But when I cleaned my working copy, that failed too.

For fixing this issue, install SQLite (download it here). Go to your SVN working directory, you’ll find a wc.db (the database where SVN keeps it’s date) in the .svn directory.

run the command

sqlite /pathto/wc.db "select * from work_queue"

You’ll probably see some records containing filenames you just touched. The records can be safely deleted. Run this:

sqlite /pathto/wc.db "delete from work_queue"

And you’re good to go! It’ll probably save you the trouble of deleting your working copy and checking it all out again.

My Whitebook about Node.js is published

My latest Whitebook is published on the Whitehorses knowledge site. It’s written in Dutch though.

If you’re looking for some in-depth articles in English, check out the Whitehorses blogposts about Node.js:

Node.js == serverside Javascript

http://blog.whitehorses.nl/2016/06/23/node-js-tutorial-part-1-setting-up-your-environment/

http://blog.whitehorses.nl/2016/04/05/use-sse-and-node-js-to-show-jet-chart-data/

 

Using MongoDB on a Raspberry Pi

Recently I hooked up my Raspberri Pi 3 as a small testing server for my MEAN stack (mainly Node.js and MongoDB in my case). That works great and I was surprised how fast this setup is on a relatively small pc.

But while utilising the more advanced features of MongoDB I found out they didn’t work as expected. The problem is, Raspbian Jessy has a 32bit kernel and MongoDB with versions higher than 3.0 only support 64bit. Hence, on Raspbian a 2.x version (32bit) is provided which of course lacks the newer features. So if you want to use the full potential of MongoDB, you’ll have to find another OS with 64bit support. I suggest an older laptop or desktop running Linux Mint! There is a tutorial for installing the right MongoDB version on Debian here.

By using this site you acknowledge the use of cookies (which are mostly harmless, btw) More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below you are agreeing to these settings.

Close