如何在查询中使用Firestore DocumentID __name__

时间:2020-08-02 09:43:53

标签: go google-cloud-firestore gcloud

我计划使用“ in”查询选择器根据ID列表返回多个文档。但是,对于这个示例,我简化了使用“ ==“:

#include "OstorUSBLib.h"
#include<iostream>
#include <Dbt.h>

#define CLS_NAME L"USB_LISTENER_CLASS"
#define HWND_MESSAGE     ((HWND)-3)
#define HID_CLASSGUID { 0x53f5630d, 0xb6bf, 0x11d0,{ 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b }} //volume
//#define HID_CLASSGUID { 0x53f56307, 0xb6bf, 0x11d0,{ 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b }} //disk
static const GUID GuidDevInterfaceList[] = {
    { 0xa5dcbf10, 0x6530, 0x11d2,{ 0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed } },//GUID_DEVINTERFACE_USB_DEVICE
    { 0x53f5630d, 0xb6bf, 0x11d0,{ 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b } },//GUID_DEVINTERFACE_VOLUME
    { 0x53f56307, 0xb6bf, 0x11d0,{ 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b } },//GUID_DEVINTERFACE_DISK
    { 0xad498944, 0x762f, 0x11d0,{ 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c } }//GUID_NDIS_LAN_CLASS
};
LRESULT OstorUSBLib::message_handler(HWND__ * hwnd, UINT uint, WPARAM wparam, LPARAM lparam)
{
    switch (uint)
    {
    case WM_NCCREATE: // before window creation
        return true;
        break;

    case WM_CREATE:
    {
        
        //works. Receives broadcast disk information
        DEV_BROADCAST_DEVICEINTERFACE NotificationFilter = {0};
        NotificationFilter.dbcc_size = sizeof(PDEV_BROADCAST_DEVICEINTERFACE);
        NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
        LPCREATESTRUCT params = (LPCREATESTRUCT)lparam;
        GUID InterfaceClassGuid = *((GUID*)params->lpCreateParams);

        for (int i = 0; i < sizeof(GuidDevInterfaceList); i++) {
            NotificationFilter.dbcc_classguid = GuidDevInterfaceList[i];

            HDEVNOTIFY dev_notify = RegisterDeviceNotification(hwnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
            if (dev_notify == NULL) {     // Handle the error by returning correct error in LRESULT format and remove throw...
                throw std::runtime_error("Could not register for device Notifications!");
            }
        }
        
    }
        break;
    case WM_DEVICECHANGE:
    {
        PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lparam;
        PDEV_BROADCAST_DEVICEINTERFACE_W lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE_W)lpdb;
        
            std::cout << lpdb->dbch_devicetype<<std::endl;
    }

    }
    return LRESULT();
}


void OstorUSBLib::RegisterListener()
{
    HWND hWnd = NULL;
    WNDCLASSEXW wx = {0};
    wx.cbSize = sizeof(WNDCLASSEXW);
    wx.lpfnWndProc = reinterpret_cast<WNDPROC>(message_handler);
    wx.hInstance = reinterpret_cast<HINSTANCE>(GetModuleHandle(0));
    wx.style = CS_HREDRAW || CS_VREDRAW;
    wx.hbrBackground = (HBRUSH)(COLOR_WINDOW);
    wx.lpszClassName = CLS_NAME;

    if(RegisterClassExW(&wx))
    {
        GUID guid = HID_CLASSGUID;
        hWnd = CreateWindowW(
            CLS_NAME, L"DevNotifWnd", WS_ICONIC, 0, 0, CW_USEDEFAULT, 0, HWND_MESSAGE,
            NULL, GetModuleHandle(0), (void*)&guid
        );
    }
    else
    {
        throw std::runtime_error("Could not create message window!");
    }
    std::cout << "waiting for new devices.\n";
    MSG msg;

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

输出:

collection := server.Firestore.Collection("foo")

// Add a document:

ref, _, err := collection.Add(ctx, map[string]string{"a": "b"})
if err != nil {
    panic(err)
}

// Here's the document ID:

fmt.Println("ref.ID", ref.ID)

// Get all the documents in the collection just to check it's there:

allDocs, err := collection.Query.Documents(ctx).GetAll()
if err != nil {
    panic(err)
}
fmt.Println("len(allDocs):", len(allDocs))

// Check our document is the one in the collection:

fmt.Println("allDocs[0].Ref.ID", allDocs[0].Ref.ID)

// Get the document using __name__ query:

docQuery, err := collection.Query.Where(firestore.DocumentID, "==", ref.ID).Documents(ctx).GetAll()
if err != nil {
    panic(err)
}
fmt.Println("len(docQuery):", len(docQuery))

据我所知,上面的代码应在查询中返回一个文档。我是否错误地使用了DocumentID(“ __名称__”)选择器?

1 个答案:

答案 0 :(得分:1)

好的,我已经解决了。将最终查询更改为:

Where(firestore.DocumentID, "==", ref)

...例如在模拟器和正式版Firestore中都可以传递整个*firestore.DocumentRef而不是string

在两个环境中传递字符串均不会返回任何结果,但是在使用仿真器时,它会静默失败并且不返回错误。