在Mongodb java中搜索标签

时间:2013-05-04 00:04:58

标签: java mongodb search tags

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

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

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

------------------------------- CODE --------------- ------------------------------------

protected void search(String term){

    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");

                }

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

1 个答案:

答案 0 :(得分: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