我是MongoDB和C#的新手。我不知道如何从我的收藏中获取所有数据" kljenti"。
任何人都可以帮助我吗?
以下是我的代码
package com.poc.sort;
/**
* @author voyger_india
*
*/
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
public class TestChapterSort {
public static void main(String[] args) {
List<String> chapters = new ArrayList<String>();
chapters.add("1.1");
chapters.add("1.2");
chapters.add("1");
chapters.add("1.3");
chapters.add("1.1.1");
chapters.add("5.6");
chapters.add("1.1.10");
chapters.add("4");
chapters.add("1.1.9");
chapters.add("1.2.1.10");
chapters.add("2.1.1.4.5");
chapters.add("1.2.1.9");
chapters.add("1.2.1");
chapters.add("2.2.2");
chapters.add("1.2.1.11");
TestChapterSort wer = new TestChapterSort();
System.out.println(wer.sortChapters(chapters));
}
private List<String> sortChapters(List<String> chapters) {
List<String> sortedChapters = new ArrayList<String>(0);
int index;
for (String currChapter : chapters) {
if (!sortedChapters.contains(currChapter)) {
index = getInsertIndex(sortedChapters, currChapter);
sortedChapters.add(index, currChapter);
System.out.println(sortedChapters);
}
}
return sortedChapters;
}
private int getInsertIndex(List<String> sortChapters, String currChapter) {
int insertIndex = 0;
if (!CollectionUtils.isEmpty(sortChapters)) {
int compChapterSub;
int currChapterSub;
String[] currChapterAr = currChapter.split("\\.");
for (String compChapter : sortChapters) {
String[] compChapterAr = compChapter.split("\\.");
for (int subLvl = 0; subLvl < 5; subLvl++) {
compChapterSub = parseToInt(compChapterAr, subLvl);
currChapterSub = parseToInt(currChapterAr, subLvl);
if (compChapterSub == currChapterSub) {
continue;
} else if (compChapterSub == 0 && currChapterSub == 0) {
break;
} else if (compChapterSub > currChapterSub) {
if (checkIfProper(subLvl, compChapterAr, currChapterAr)) {
return insertIndex;
}
}
}
insertIndex++;
}
} else {
return 0;
}
return insertIndex;
}
private int parseToInt(String[] subChapter, int subLvl) {
try {
return Integer.parseInt(subChapter[subLvl]);
} catch (ArrayIndexOutOfBoundsException ae) {
return 0;
}
}
private boolean checkIfProper(int subLvl, String[] compChapterAr, String[] currChapterAr) {
int subLvlCk = subLvl - 1;
int compChapterSub;
int currChapterSub;
while (subLvlCk > -1) {
compChapterSub = parseToInt(compChapterAr, subLvlCk);
currChapterSub = parseToInt(currChapterAr, subLvlCk);
if (compChapterSub < currChapterSub) {
return false;
}
subLvlCk--;
}
return true;
}
}
答案 0 :(得分:1)
应该是这样的:
var allDocuments = collection.Find(new 。BsonDocument())ToListAsync()结果。