0

我有 3 个不同的集合,在我的脚本中有不同的内容:图像、音频和视频。

在我放入数据库的每个元素中,我添加了一个标签。

当我尝试搜索标签(我添加每个集合的文件)时,我只能找到图像集合的标签:

- - - - - - - - - - - - - - - -代码 - - - - - - - - - ---------------------------------

受保护的无效搜索(字符串术语){

    tagCounter = 0;
    DBCollection image = db.getCollection("p");
    DBCollection audio = db.getCollection("a");
    DBCollection video = db.getCollection("video");

    String search = searchField.getText();
    search.trim().toLowerCase();    
    BasicDBObject tagQuery= new BasicDBObject();
    tagQuery.put("tags", search);

    DBCursor cursor = collection.find(tagQuery);
    tagQuery.put("tags", search);

                cursor = image.find(tagQuery);
                while(cursor.hasNext()) {
                     results.addElement( cursor.next().toString());
                     tagCounter++; 
                    searchField.setText(null);
                    }

                cursor = audio.find(tagQuery);
                while(cursor.hasNext()) {
                        results.addElement(cursor.next());
                         tagCounter++; 
                        searchField.setText(null);
                     }

                 cursor = video.find(tagQuery);    
                 while(cursor.hasNext()) {
                         results.addElement( cursor.next().toString()) ;
                         tagCounter++; 
                        searchField.setText(null);
                 }

                 JOptionPane counter = new JOptionPane();   
                counter.showMessageDialog(resultList, "Search gave " + tagCounter + " files");

                }

任何人都可以帮助新手吗?:)

4

1 回答 1

0

该代码对我来说非常有效,除了您有很多对未声明/定义的事物的引用以及您.toString()在音频集合中丢失的事实。

简而言之,数据以相同的方式从所有集合中获取,您需要确保在代码中执行的操作是检查searchField.setText(null);行的作用 - 因为您对第一个集合进行了处理,但对接下来的两个集合却没有告诉我您可能正在清除代码所需的内容。

最好的办法是在整个过程中使用大量“调试”语句,而不仅仅是在最后。这是您的代码的简化版本(我在每个集合中放置了一个匹配的文档):

int tagCounter = 0;
DBCollection image = db.getCollection("p");
DBCollection audio = db.getCollection("a");
DBCollection video = db.getCollection("video");
String search = "tag1";
search.trim().toLowerCase();
BasicDBObject tagQuery= new BasicDBObject();
tagQuery.put("tags", search);
DBCursor cursor = null;

cursor = image.find(tagQuery);
while(cursor.hasNext()) {
     System.out.println( cursor.next().toString());
     tagCounter++;
}
System.out.println(tagCounter + " matches found in image");

cursor = audio.find(tagQuery);
tagCounter = 0;
while(cursor.hasNext()) {
     System.out.println( cursor.next().toString());
     tagCounter++;
     }

System.out.println(tagCounter + " matches found in audio");

cursor = video.find(tagQuery);
tagCounter = 0;
while(cursor.hasNext()) {
     System.out.println( cursor.next().toString());
     tagCounter++;
 }
System.out.println(tagCounter + " matches found in video");

我的输出是:

{ "_id" : { "$oid" : "5186a59151058e0786e90eee"} , "tags" : [ "tag1" , "tag2"]}
1 matches found in image
{ "_id" : { "$oid" : "5186a59851058e0786e90eef"} , "tags" : [ "tag1" , "tag2"]}
1 matches found in audio
{ "_id" : { "$oid" : "5186a5a851058e0786e90ef0"} , "tags" : [ "tag1" , "tag2"]}
1 matches found in video
于 2013-05-05T18:45:47.517 回答