1

I have a collection. In this collection I have only one document like this:

_id:
emails: []

I would like to delete the values in emails array.

My code under /models/askForReferral.js:

const mongoose = require('mongoose');

const WaitingForReferral = mongoose.model('WaitingForReferral', new mongoose.Schema({

    emails: []

}));

exports.WaitingForReferral = WaitingForReferral;

My code under /routes/referralEmail.js:

const { WaitingForReferral } = require('../models/askForReferral');
const express = require('express');
const router = express.Router();

router.post('/', async (req, res) => {

    WaitingForReferral.update(
        { },
        { $unset: { emails: "" } }
     )

});

When I run my code, it runs without error, but emails array is not emptied.

Any idea?

1 Answer 1

1

Use $set operator with blank array [] and also mongoose query returns promise you need to use await to execute it

await WaitingForReferral.update(
  { },
  { $set: { emails: [] } }
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.