【问题标题】:Deleting values in array (MongoDB and mongoose)删除数组中的值(MongoDB 和猫鼬)
【发布时间】:2019-09-04 17:21:39
【问题描述】:

我有一个收藏。在这个集合中,我只有一个这样的文档:

_id:
emails: []

我想删除 emails 数组中的值。

我在 /models/askForReferral.js 下的代码:

const mongoose = require('mongoose');

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

    emails: []

}));

exports.WaitingForReferral = WaitingForReferral;

我在 /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: "" } }
     )

});

当我运行我的代码时,它运行没有错误,但 emails 数组没有被清空。

有什么想法吗?

【问题讨论】:

    标签: arrays node.js mongodb mongoose


    【解决方案1】:

    $set 运算符与空白数组[] 一起使用,并且猫鼬查询返回承诺您需要使用await 来执行它

    await WaitingForReferral.update(
      { },
      { $set: { emails: [] } }
    )
    

    【讨论】:

      猜你喜欢
      • 2021-09-03
      • 2021-06-14
      • 2017-07-05
      • 2022-08-02
      • 1970-01-01
      • 2018-05-28
      • 2016-05-25
      • 2020-06-10
      • 2016-01-03
      相关资源
      最近更新 更多