如何在 RethinkDB 中获取给定谓词上元素的索引

How to get index of an element on given predicate in RethinkDB

你好,我有一个 table,我想在给定文档之后获取所有 documents

问题是我还没有看到 skipwhile 功能,你给出了 predicate。 所以我必须求助于首先找到我给定的索引 element.How 是否可以实现?

Ex: 给定文档 {"a":1 },{"b":2},{"a":33},{"c":44},{"d":33} i想获取最后一个 a 之后的所有元素,所以我想得到:[ {"c":44},{"d":33}]

这是否可以通过 RethinkDB 驱动程序实现,或者我需要在完成所有 table.

后解决这个问题

尝试类似下面的代码:

            List<KeyValuePair<string, int>> input = new List<KeyValuePair<string, int>>() {
                new KeyValuePair<string, int>("a", 1),
                new KeyValuePair<string, int>("b", 2),
                new KeyValuePair<string, int>("a",33),
                new KeyValuePair<string, int>("c",44),
                new KeyValuePair<string, int>("d",33)
            };

            int skipIndex = input.Select((x, i) => new { value = x, index = i }).Where(x => x.value.Key == "a").Last().index;
            List<KeyValuePair<string, int>> results = input.Skip(skipIndex + 1).ToList();

我试着按照你提出问题的例子来解决你的问题:

-I've got a Folder with the following files/documents{a.txt,b.txt,c.txt,d.txt,f.txt}

-I want an Array of all files after the -count- (look at the GetFilesAfterFilename function) time occurrence of the name "b.txt"


using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo DirectoryOfDocuments = new DirectoryInfo(Environment.CurrentDirectory+ @"\documents");//Get direcory with your files(Ex: a.txt,b.txt,c.txt,d.txt,..)
            FileInfo[] OldFileInfo = DirectoryOfDocuments.GetFiles();//Get all files out of your directory

            FileInfo[] NewFileInfo = GetFilesAfterFilename(OldFileInfo, 1, "c.txt");

            foreach (FileInfo fi in NewFileInfo)
            {
                Console.WriteLine(fi.ToString());
            }
            Console.Read();
        }

        static FileInfo[] GetFilesAfterFilename(FileInfo[] OriginArray, int count, string FileName) {
            List<FileInfo> NewFileInfo = new List<FileInfo>();
            int FileNameOccurrence = 0;
            foreach (FileInfo fi in OriginArray)
            {
                if (FileNameOccurrence == count){//Add all Files from OriginArray after [count] time occurrence of somewhat file/document
                    NewFileInfo.Add(fi);
                }else if (fi.ToString()==FileName) {
                    FileNameOccurrence++;
                }
            }

            return NewFileInfo.ToArray();
        }
    }
}


控制台输出
d.txt
f.txt


So i have to resort to first finding the index of my given element. How is that achieveable?

您可以使用offsetsOf()函数。它 returns 与您搜索的元素匹配的索引数组。您可以通过以下方式获取{"a":33}文档的索引:

r.expr([{"a":1 },{"b":2},{"a":33},{"c":44},{"d":33}])
  .offsetsOf({"a":33}).nth(0)

要进一步迭代其余元素,您可以使用 r.range() 函数,如下所示:

r.expr([{"a":1 },{"b":2},{"a":33},{"c":44},{"d":33}])
    .do(function(docs){
      return docs.offsetsOf({"a":33}).nth(0)
        .do(function(index){
          return r.range(index.add(1), docs.count())
            .map(function(i){
              return docs.nth(i)
            })
        })
    })