【问题标题】:Searching for tags in Mongodb java在 Mongodb java 中搜索标签
【发布时间】:2013-05-04 00:04:58
【问题描述】:

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

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

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

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

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

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

                }

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

【问题讨论】:

  • 不确定这是不是问题,但可能是。 search.trim().toLowerCase(); 应该是 search = search.trim().toLowerCase();。难道数据库中的标签没有被正确规范化?

标签: java mongodb search tags


【解决方案1】:

代码对我来说完美无缺,除了你有很多没有声明/定义的东西的引用,而且你在音频集合中缺少.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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    相关资源
    最近更新 更多