These are chat archives for Automattic/mongoose

23rd
Oct 2018
Ben Mezger (seds)
@benmezger
Oct 23 2018 14:42
I am trying to do an aggregation, but I wonder how can I get the result from query?
OrderSchema.statics.countTotalOrders = function (_id) {
  const id = mongoose.Types.ObjectId(_id);
  const query = mongoose.model('Quiz').aggregate([
    {
      $lookup: {
        from: 'orders',
        localField: 'order',
        as: 'user_order',
        foreignField: '_id',
      }
    },
    {
      $match: {
        $or: [
          { 'user_order.paymentStatus': 'PENDING' },
          { 'user_order.paymentStatus': 'ACCEPTED' }
        ]
      }
    },
    {
      $unwind: '$user_order',
    },
    {
      $match: {
        status: 'PASSED',
      }
    },
    {
      $group: {
        _id: '$user',
        tickets: {
          $sum: {
            $size: '$user_order.tickets',
          }
        }
      }
    },
    {
      $match: {
        _id: id,
      }
    }
  ]);
  return query;
};
Grant Allor
@gallor
Oct 23 2018 14:54

Does anyone know of a good way to log connection status to each node in a replica set and if it can't connect, produce logging? I found there's an object at mongoose.connection.db.s.topology.s.replset that gave me a list of what servers were connected and what servers were unreachable inside the object when stopped at a breakpoint but I'd like to find some logs as to why they're unreachable, if there are any.
Also, I've been trying to log the primary connected server off the pickedServer event and haven't had any luck using this code:

        mongoose.connect(`${uri}/${dbName}/?replicaSet=${replicaSet}`, {
          db: {
            readPreference: "nearest"
          },
          replset: {
            debug: true
          }
        });
        mongoose.set('debug', true);
        mongoose.connection.db.s.topology.s.replset.on('pickedServer', (readPref, server) => {
          console.log('Picked', server.s.serverDescription);
        });
        mongoose.connection.on("error", (error) => {
          log("Unable to connect to Schedules MongoDB", {dbName, uri, error});
          reject(error);
        });
        mongoose.connection.once("open", () => {
          const db = mongoose.connection;
          log("Connected to Schedules MongoDB", {dbName, uri});
          const Schedules = db.model("schedule", scheduleSchema);
          const ProductSchedules = db.model("product_schedule", productScheduleSchema);
          const Exceptions = db.model("exception", exceptionSchema);
          const ProductExceptions = db.model("product_exception", productExceptionSchema);
          const ImportedCCFiles = db.model("imported_cc_files", importedCCFilesSchema);
...
});

I saw the pickedServer event is not available outside the open event callback, but is once inside but it's still not triggered. Probably missing something really obvious... Thanks!

Kev
@lineus
Oct 23 2018 18:56
@benmezger are you executing the query by explicitly calling .exec or .then or await query?