如何使用MongooseJS删除MongoDB属性数组中的元素?

时间:2017-04-05 02:48:11

标签: node.js mongodb mongoose mongoose-schema

我无法删除作为MongoDB模型属性的数组内部的元素。 请记住这是一个NodeJS模块 mongooseJS ,而不是真正的MongoDB,因此功能不一样..

目标:从 statusLiked 数组中删除对象。 | 我还确认status.id的值是正确的。

型号:

 Const userSchema = new mongoose.Schema({
  myStatus: Array,
  statusLiked: Array,
 )};

删除:  1.删​​除状态(有效)。 2.从User.statusLiked(无工作)中删除状态。

exports.deleteStatus = (req, res, next) => {
  var CurrentPost = req.body.statusid; // sends in the status.id
  Status.remove({ _id: CurrentPost }, (err) => {
    if (err) { return next(err); }
    // vvvv this vvv
    User.update( {id: req.user.id}, { $pullAll: {_id: CurrentPost }, function(err) { console.log('error: '+err) } });

    req.flash('success', { msg: 'Status deleted.' });
    res.redirect('/');
  });

};

会发生什么:从数据库中删除特定状态(对象)。但状态仍保留在User.statusLiked数组中。

我想要发生的事情:要从User.statusLiked数组中删除的状态以及要从数据库中删除的状态。然后,重新加载页面并显示通知。

1 个答案:

答案 0 :(得分:0)

我以某种方式工作了。工作代码:

int Test::allServicesWMIData()
{
    HRESULT hres;

    // Initialize COM.
    hres = CoInitializeEx(0, COINIT_APARTMENTTHREADED);

    if (FAILED(hres))
    {
        emit initComLibError(QString(QObject::tr("Failed to initialize COM library. Error code =") + " 0x%1").arg(hexErrorData(hres)));
        return 1;              // Program has failed.
    }

    IWbemLocator *pLocator = 0;

    hres = CoCreateInstance(
        CLSID_WbemLocator,
        0,
        CLSCTX_INPROC_SERVER,
        IID_IWbemLocator, (LPVOID *)&pLocator);

    if (FAILED(hres))
    {
        emit errorCreateIWbemObject(QString(QObject::tr("Failed to create IWbemLocator object. Error code =") + " 0x%1").arg(hexErrorData(hres)));
        CoUninitialize();
        return 1;       // Program has failed.
    }

    IWbemServices *pService = 0;

    hres = pLocator->ConnectServer(
        _bstr_t(L"ROOT\\CIMV2"), // WMI namespace
        NULL,                    // User name
        NULL,                    // User password
        0,                       // Locale
        NULL,                    // Security flags
        0,                       // Authority
        0,                       // Context object
        &pService                    // IWbemServices proxy
    );

    if (FAILED(hres))
    {
        emit errorRootConnection(QString(QObject::tr("Could not connect. Error code =") + " 0x%1").arg(hexErrorData(hres)));
        pLocator->Release();
        CoUninitialize();
        return 1;                // Program has failed.
    }

    hres = CoSetProxyBlanket(
        pService,                         // the proxy to set
        RPC_C_AUTHN_WINNT,            // authentication service
        RPC_C_AUTHZ_NONE,             // authorization service
        NULL,                         // Server principal name
        RPC_C_AUTHN_LEVEL_CALL,       // authentication level
        RPC_C_IMP_LEVEL_IMPERSONATE,  // impersonation level
        NULL,                         // client identity
        EOAC_NONE                     // proxy capabilities
    );

    if (FAILED(hres))
    {
        emit errorProxyBlanket(QString(QObject::tr("Could not set proxy blanket. Error code =") + " 0x%1").arg(hexErrorData(hres)));
        pService->Release();
        pLocator->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }

    IEnumWbemClassObject* pEnumerator = NULL;
    hres = pService->ExecQuery(
        bstr_t("WQL"),
        bstr_t("SELECT * FROM Win32_Service"),
        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
        NULL,
        &pEnumerator);

    IWbemClassObject *pclsObj = NULL;

    if (FAILED(hres))
    {
        emit errorProcessQuery(QString(QObject::tr("Query for processes failed. Error code =") + " 0x%1").arg(hexErrorData(hres)));
        pService->Release();
        pLocator->Release();
        CoUninitialize();
        return 1;               // Program has failed.
    }
    else
    {
        ULONG uReturn = 0;

        while (pEnumerator)
        {
            hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);

            VARIANT serviceName;
            VARIANT servicePath;
            VARIANT serviceID;
            VARIANT serviceType;
            VARIANT serviceState;
            VARIANT serviceStatus;
            VARIANT serviceErrorControl;
            VARIANT serviceStartMode;
            VARIANT serviceWaitHint;
            VARIANT serviceExitCode;

            if (hres != 0) {
                emit hardwareDataNotAvailable();
                break;
            } else {
                pclsObj->Get(L"Caption", 0, &serviceName, 0, 0);
                pclsObj->Get(L"PathName", 0, &servicePath, 0, 0);
                pclsObj->Get(L"ProcessId", 0, &serviceID, 0, 0);
                pclsObj->Get(L"ServiceType", 0, &serviceType, 0, 0);
                pclsObj->Get(L"State", 0, &serviceState, 0, 0);
                pclsObj->Get(L"Status", 0, &serviceStatus, 0, 0);
                pclsObj->Get(L"ErrorControl", 0, &serviceErrorControl, 0, 0);
                pclsObj->Get(L"StartMode", 0, &serviceStartMode, 0, 0);
                pclsObj->Get(L"WaitHint", 0, &serviceWaitHint, 0, 0);
                pclsObj->Get(L"ExitCode", 0, &serviceExitCode, 0, 0);
            }

            QString userServiceName = QString::fromWCharArray(serviceName.bstrVal);
            QString userServicePath = QString::fromWCharArray(servicePath.bstrVal);
            QString userServiceID = QString::number(serviceID.uintVal);
            QString userServiceType = QString::fromWCharArray(serviceType.bstrVal);
            QString userServiceState = QString::fromWCharArray(serviceState.bstrVal);
            QString userServiceStatus = QString::fromWCharArray(serviceStatus.bstrVal);
            QString userServiceErrorControl = QString::fromWCharArray(serviceErrorControl.bstrVal);
            QString userServiceStartMode = QString::fromWCharArray(serviceStartMode.bstrVal);
            QString userServiceWaitHint = QString::number(serviceWaitHint.uintVal);
            QString userServiceExitCode = QString::number(serviceExitCode.uintVal);

            emit appAllServicesData(userServiceName, userServicePath, userServiceID, userServiceType, userServiceState, userServiceStatus, userServiceErrorControl,
            userServiceStartMode, userServiceWaitHint, userServiceExitCode);

            VariantClear(&serviceName);
            VariantClear(&servicePath);
            VariantClear(&serviceID);
            VariantClear(&serviceType);
            VariantClear(&serviceState);
            VariantClear(&serviceStatus);
            VariantClear(&serviceErrorControl);
            VariantClear(&serviceStartMode);
            VariantClear(&serviceWaitHint);
            VariantClear(&serviceExitCode);
        }
    }

    // Cleanup
    pService->Release();
    pLocator->Release();
    //pEnumerator->Release(); - Clang Static Analyzer displays issue - called C++ object pointer is null
    //pclsObj->Release(); - Clang Static Analyzer displays issue - called C++ object pointer is null
    CoUninitialize();
    emit finished();
    return 0;   // Program successfully completed.
}