Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are all the mongoose events and where are they documented?

There are various events I can handle with mongoose, e.g.

mongoose.connection.on("connecting", () => this.onConnecting());

Some of those I've encountered:

  • connecting
  • error
  • connected
  • open
  • reconnected
  • disconnected

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?

like image 849
lonix Avatar asked Feb 01 '19 19:02

lonix


People also ask

What are transactions in Mongoose?

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.

How does a Mongoose work?

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.

What is Mongoose schema explain with example?

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}, });

How does Mongoose know which collection?

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.


2 Answers

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:

  • @event connecting: Emitted when connection.openUri() is executed on this connection.
  • @event connected: Emitted when this connection successfully connects to the db. May be emitted multiple times in reconnected scenarios.
  • @event open: Emitted after we connected and onOpen is executed on all of this connections models.
  • @event disconnecting: Emitted when connection.close() was executed.
  • @event disconnected: Emitted after getting disconnected from the db.
  • @event close: Emitted after we disconnected and onClose executed on all of this connections models.
  • @event reconnected: Emitted after we connected and subsequently disconnected, followed by successfully another successfull connection.
  • @event error: Emitted when an error occurs on this connection.
  • @event fullsetup: Emitted in a replica-set scenario, when primary and at least one seconaries specified in the connection string are connected.
  • @event all: Emitted in a replica-set scenario, when all nodes specified in the connection string are connected.
like image 152
ajbieber Avatar answered Nov 10 '22 15:11

ajbieber


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.

like image 45
MaieonBrix Avatar answered Nov 10 '22 13:11

MaieonBrix