Using Database Triggers to Update a Single Field

Trying to use database triggers to bump a specific document field into another document leaving a reference when the size get too large for the original document. Best we can find issues inline

Hi Paul,

Assuming you are referring to the size of an array being too big, and not your document.

  1. Create a database trigger that fires on update.
  2. Create a function that adds a document with the data you expect to move
  3. Add a match expression using dot notation to check the size of the array (using $size and $gt)

However, this may need a little more thought into how you’re modeling your data - you can learn more about it in the data modeling university course,

You may also want to use “references” to create a one-to-many relationship with your data if that fits your needs.

Thanks @Sumedha_Mehta1 Actually it’s not an array, it would be an object.

Imagine a userDoc. Then the user uploads a photo and we put it into the userDoc so we can get both the photo info and userDoc back in one single request. We want to know the like counts of the pic so we have that too, but if a power user posts an image all of a sudden we blow out the userDoc (size) adding all 100k plus user ID’s to the userDoc. So generally we know after a few hundred that it’s going to be popular, so want to pre-emptively put the photo fields and uesrID object of those who liked it, into it’s own doc leaving the new doc reference in the userDoc. Until the next photo comes along. Putting the picture into it’s own doc 100% of the time needlessly when the userDoc can easily handle a few hundred (or much more) likes would create a secondary doc lookup, in most scenarios, needlessly. Thanks!