我想将MongoDB中的所有ID添加到使用Java的数组中。如何从JAVA获取ID或任何数据表格以及放入数组?
PS:我是nosql数据库结构的新手,我使用的是MongoDb 3.2
到目前为止,代码是这样的。public void ArrayEx(){
MongoClient mongoClient = new MongoClient("localhost",27017);
MongoDatabase database = mongoClient.getDatabase("dbTest");
MongoCollection<Document> collection =database.getCollection("colTest");
MongoCursor<Document> cursor = collection.find().iterator();
while(cursor.hasNext()) {
//What should I add here?
}
}
答案 0 :(得分:0)
如果只想要id,可以使用mongodb distinct函数
template: '<input type="text" [ngValue]="formattedValue" (ngValueChange)="checkValue($event)">',
formattedValue:string = '';
constructor(private cdRef:ChangeDetectorRef) {}
checkValue(event) {
if(event == /* invalid */) {
this.cdRef.detectChanges();
} else {
this.formattedValue = event;
}
}
答案 1 :(得分:0)
按如下方式修改方法:
public void ArrayEx() {
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("dbTest");
MongoCollection<Document> collection = database.getCollection("colTest");
MongoCursor<Document> cursor = collection.find().iterator();
ArrayList<String> listOfIDS = new ArrayList<>();
while (cursor.hasNext()) {
listOfIDS.add(cursor.next().getObjectId("_id").toString());
}
String ids[] = new String[listOfIDS.size()];
ids = listOfIDS.toArray(ids);
}