generic3892372
generic3892372

Reputation: 206

mongo deleteMany; how to debug result?

I'm using Node with Mongoose.

Here's the deleteMany() I'm attempting to debug:

Code:

  const result = await Follow.deleteMany({
    $or: [
      { userFollowingAnother: req.user.id },
      { userThatIsFollowed: req.user.id },
    ],
  });

  console.log("result: " + result.acknowledged);

Details:

The result is not returning any value unfortunately. (returns undefined) Also, the deleteMany is not working, as the documents still exist in the "Follow" collection afterwards.

The official documentation generally indicates that a response is provided, and even provided an example, but I can't seem to access the result object's data or message.

Goal: A working query, but more importantly, a method for debugging the result, or at least accessing some sort of result.

Update - Fixed: I was missing a simple await statement prior to the Mongoose call. I've updated the code to reflect the fixed version, as my question had to do with debugging.

Upvotes: 1

Views: 314

Answers (1)

ezio4df
ezio4df

Reputation: 4195

From https://mongoosejs.com/docs/api/mongoose.html#mongoose_Mongoose-set

Use Something like this to,

Enable logging collection methods + arguments to the console

mongoose.set('debug', true)

Use custom function to log collection methods + arguments

mongoose.set('debug', function(collectionName, methodName, ...methodArgs) {}); 

Upvotes: 1

Related Questions