There are various events I can handle with mongoose, e.g.
mongoose.connection.on("connecting", () => this.onConnecting());
Some of those I've encountered:
I can't find where these are documented. I thought that maybe they weren't part of mogoose, but rather of mongodb itself, but I couldn't find them on the mongo site either.
Where can I find these events (and others) documented?
Transactions in Mongoose. Transactions are new in MongoDB 4.0 and Mongoose 5.2. 0. Transactions let you execute multiple operations in isolation and potentially undo all the operations if one of them fails.
With Mongoose, you would define a Schema object in your application code that maps to a collection in your MongoDB database. The Schema object defines the structure of the documents in your collection. Then, you need to create a Model object out of the schema. The model is used to interact with the collection.
A schema in Mongoose maps to a MongoDB collection and defines the format for all documents on that collection. All properties inside the schema must have an assigned SchemaType . For example, the name of our Person can be defined this way: const PersonSchema = new mongoose. Schema({ name: { type: String}, });
Mongoose uses the model name, as passed when it was created: mongoose. model("User", UserSchema) , converted to lower case and with an 's' appended. For the model User it uses the collection users by default. You can change this by explicitly specifying the collection name in the schema.
You can find it on github in the source code. https://github.com/Automattic/mongoose/blob/master/lib/connection.js
There are many more events listed than the given answer, here they are:
connecting
: Emitted when connection.openUri()
is executed on this connection.connected
: Emitted when this connection successfully connects to the db. May be emitted multiple times in reconnected
scenarios.open
: Emitted after we connected
and onOpen
is executed on all of this connections models.disconnecting
: Emitted when connection.close()
was executed.disconnected
: Emitted after getting disconnected from the db.close
: Emitted after we disconnected
and onClose
executed on all of this connections models.reconnected
: Emitted after we connected
and subsequently disconnected
, followed by successfully another successfull connection.error
: Emitted when an error occurs on this connection.fullsetup
: Emitted in a replica-set scenario, when primary and at least one seconaries specified in the connection string are connected.all
: Emitted in a replica-set scenario, when all nodes specified in the connection string are connected.By looking into the source code if found where the events are centralized :
https://github.com/Automattic/mongoose/blob/2176150b3d463747ba66b76e37504ee8ffc3f738/lib/connectionstate.js
here is a copy-pasta :
const disconnected = 'disconnected';
const connected = 'connected';
const connecting = 'connecting';
const disconnecting = 'disconnecting';
const uninitialized = 'uninitialized';
UPDATE :
Check @ajbieber 's answer for the full list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With