DEV Community


Tracking and saving updates in a collection in MongoDB

samuanv profile image Samuel Andreo ・1 min read

Hello everyone! I am Samuel and I am a young web developer. Actually I am working in a project to help my family's business.
The system is about access control to a vehicle with QR codes. It is composed by a web application (Angular6), mobile application (Ionic3) and REST API (NodeJS) connected to a noSQL DB (MongoDB).

One of my collections is Reservations and the document is structured as follows:

_id: number,
serviceId: number,
datetimePaid: Date,
responsible: Person,
state: string,
tickets: Tickets[],

And Tickets is something like:

price: number,
passenger: Person,
roundtrip: boolean

A reservation could be modified by users with differente roles (with higher or lower privileges) but I need to track whenever an user has modified some field of the document to give the administrator a good way to solve possible problems.
The operations or fields that could be updated are:

  • datetimePaid
  • responsible (replace it with another Person)
  • state
  • tickets[i].passenger
  • Add or remove elements from the tickets array

I haven't find any native solution provided by MongoDB to it, and I have couple of ideas but I would like to hear your strategies or ways to do it.

Many thanks in advance

Discussion (1)

blake profile image
Blake Khan

To me, it sounds like a Change Stream would be ideal here! Here's the documentation:

MongoDB's official blog has a nice little introduction post with code samples too. :)

Forem Open with the Forem app