使用通配符作为路径的深层路径查询

Sam*_*ews 6 javascript firebase firebase-realtime-database

我有数据,其中我试图遵循Firebase关于扁平结构的建议,所以我没有超出我需要的数量.最终的结果是我在这样的节点中组织了引号:

quotes -> clientName -> quoteObject
Run Code Online (Sandbox Code Playgroud)

quoteObjects有一个'dateCreated'值,我希望能够像这样提取这些数据(因为当我拉出一个特定页面的所有引号的大列表时,我然后使用对象赋值来制作一个大数组要显示的对象):

  const quotesRef = firebase.database().ref('quotes');
    quotesRef.orderByChild('dateCreated').on('value', snapshot => {
       // function
    });
Run Code Online (Sandbox Code Playgroud)

不幸的是,dateCreated值超过一个级别,因此标准查询不起作用.我知道如果您知道父路径,则可以执行深层路径查询,但在我的情况下,clientName父项始终是唯一的.鉴于此,是否可以指定一种通配符路径?如果是这样,我该怎么办呢?干杯!

编辑:显示数据库示例,抱歉最初应该更具体.

quotes
    - ClientEmailOne
        -UniqueQuoteID
            - createdBy: ClientEmailOne
            - dateCreated: 1479255005172
            - email: "clientsEmail@example.com"
    - ClientEmailTwo
        -UniqueQuoteID
            - createdBy: ClientEmailTwo
            - dateCreated: 1479255005172
            - email: "clientsEmail@example.com"
     - ClientEmailThree
         -UniqueQuoteID
            - createdBy: ClientEmailThree
            - dateCreated: 1479255005172
            - email: "clientsEmail@example.com"
        -UniqueQuoteID
            - createdBy: ClientEmailThree
            - dateCreated: 1479255005172
            - email: "clientsEmail@example.com"
        -UniqueQuoteID
            - createdBy: ClientEmailThree
            - dateCreated: 1479255005172
            - email: "clientsEmail@example.com"
Run Code Online (Sandbox Code Playgroud)

Fra*_*len 5

Firebase将考虑您在其上运行查询的位置的子级。您指定用于排序/过滤的子级可以包含属性的路径,但不能包含任何通配符。

更新

使用新的数据结构,您似乎正在尝试在所有客户端以及每个客户端的所有引号中进行查询。那里有两个通配符(该位置下的“所有客户端”和“所有引号”),Firebase的查询模型不允许使用。

您当前的模型允许查询特定客户的所有报价:

ref.child("quotes").child("ClientEmailOne").orderByChild("dateCreated")
Run Code Online (Sandbox Code Playgroud)

如果要查询所有客户端上的所有引号,则需要添加一个包含所有引号的数据结构(与客户端无关):

allquotes
    -UniqueQuoteID
        - createdBy: ClientEmailOne
        - dateCreated: 1479255005172
        - email: "clientsEmail@example.com"
    -UniqueQuoteID
        - createdBy: ClientEmailTwo
        - dateCreated: 1479255005172
        - email: "clientsEmail@example.com"
     -UniqueQuoteID
        - createdBy: ClientEmailThree
        - dateCreated: 1479255005172
        - email: "clientsEmail@example.com"
    -UniqueQuoteID
        - createdBy: ClientEmailThree
        - dateCreated: 1479255005172
        - email: "clientsEmail@example.com"
    -UniqueQuoteID
        - createdBy: ClientEmailThree
        - dateCreated: 1479255005172
        - email: "clientsEmail@example.com"
Run Code Online (Sandbox Code Playgroud)

然后您可以查询:

ref.child("allquotes").orderByChild("dateCreated")
Run Code Online (Sandbox Code Playgroud)

  • 当嵌套子项位于固定路径时,Firebase可以按“仅”子项排序。因此,您可以订购`ref.child(“ quotes”)。orderByChild(“ property / under / clientName”)`,但不能订购`ref.child(“ quotes”)。orderByChild(“ property / ??? / clientName”) ` (2认同)
  • 使用NoSQL数据库时,您会发现不断发现“需要”复制数据的情况。这是很常见的,但对于大多数(面向SQL)开发人员来说却是不自然的。您越早克服我们所有人对复制数据所产生的自然焦虑,您就越早开始获得高度可扩展的读取的好处。 (2认同)