Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Mongodb java中搜索标记_Java_Mongodb_Search_Tags - Fatal编程技术网

在Mongodb java中搜索标记

在Mongodb java中搜索标记,java,mongodb,search,tags,Java,Mongodb,Search,Tags,我有3个不同的集合,脚本中有不同的内容: 图像、音频和视频 在我放入数据库的每个元素中,我都添加了一个标记 当我尝试搜索标签(我添加的每个集合的文件)时,我只能找到图像集合的标签: int tagCounter = 0; DBCollection image = db.getCollection("p"); DBCollection audio = db.getCollection("a"); DBCollection video = db.getCollection("video"); Str

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

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

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

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

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



有人能帮助新手吗?:)

这段代码对我来说非常适合,除了您有很多未声明/定义的引用,而且音频集合中缺少
.toString()

简而言之,从所有集合中以相同的方式获取数据,您需要确保在代码中执行的操作是检查
searchField.setText(null)
line确实如此-由于您在第一次收集中获得了良好的效果,但在接下来的两次收集中却没有,因此它告诉我您可能正在清除代码所需的某些内容

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

我的输出是:

{ "_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

不确定这是否是问题所在,但可能是。
search.trim().toLowerCase();
应该是
search=search.trim().toLowerCase();
。数据库中的标记是否未正确规范化?
{ "_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