Documentation Index
Fetch the complete documentation index at: https://www.esixorm.com/llms.txt
Use this file to discover all available pages before exploring further.
Inserting & Updating Models
When it comes to adding new models to the database, there are two different ways to go about it. You can either use thecreate method and pass it the attributes you want the model to have, or you can create a new instance of the model and call its save method.
createdAt property will be filled with the
current timestamp if it’s not present.
If you want to update an existing model, you can also use the save method to
persist your changes.
save on an already existing model, the updatedAt field will be
filled with the current timestamp.
Both the timestamp properties contain the current time in milliseconds since
January 1st, 1970, using JavaScript’s
Date.now
method.
First or Create
Sometimes you may want to retrieve a model by certain attributes, or create it if it doesn’t exist. ThefirstOrCreate method will attempt to locate a model
using the given filter criteria. If the model is not found in the database, a
record will be created with the attributes from the filter, plus any additional
attributes passed as a second parameter.
firstOrCreate method’s first argument contains the attributes that you
want to search for. The second argument contains the additional attributes to
add to the model if it doesn’t exist. If the second argument is not provided,
the attributes from the first argument will be used when creating the model.
Increment & Decrement
When you only need to bump a numeric field, useincrement and decrement
directly on the query builder. They translate to MongoDB’s $inc operator and
respect the current where constraints.