gpt4 book ai didi

java - 如何使用java从mongodb中的完整测试搜索结果中获取特定字段

转载 作者:可可西里 更新时间:2023-11-01 10:33:10 24 4
gpt4 key购买 nike

我的mongodb中有一些文件如下

{"Filename":"PHP Book.pdf","Author":"John" ,"Description":"This is my PHP Book"} 
{"Filename":"Java Book.html" ,"Author":"Paul" ,"Description":"This is my JAVA Book"}
{"Filename":".NET Book.doc" ,"Author":"James" ,"Description":"This is my .NET Book"}

我已经在描述字段上创建了文本索引,下面是我在描述字段上进行全文搜索的代码。

            m = new Mongo("10.0.0.26", 27017);
DB db = m.getDB("soft") ;
DBCollection col = db.getCollection("pocnew") ;
String collection = col.toString();
DBObject searchCmd = new BasicDBObject();
searchCmd.put("text", collection);
searchCmd.put("search", "php");
CommandResult commandResult = db.command(searchCmd);
System.out.println(commandResult);

我得到以下结果

{ "serverUsed" : "/10.0.0.26:27017" , "queryDebugString" : "php||||||" , "language" : "english" , "results" : [ { "score" : 0.5833333333333334 , "obj" : { "_id" : { "$oid" : "51cd7d302d45471420c1132b","Filename":"PHP Book.pdf","Author":"John" ,"Description":"This is my PHP Book"}]}}

我的要求是只显示文件名字段值,即只显示 PHP Book.pdf

请推荐我

谢谢

最佳答案

您可能需要深入研究返回的文档才能找到文件名

CommandResult commandResult = db.command(searchCmd);
BasicDBList results = (BasicDBList)commandResult.get("results");
for( Iterator< Object > it = results.iterator(); it.hasNext(); )
{
BasicDBObject result = (BasicDBObject) it.next();
BasicDBObject dbo = (BasicDBObject) result.get("obj");
System.out.println(dbo.getString("Filename"));
}

此外,如果您只需要文件名字段,您可能需要在 searchCmd 中添加项目选项以限制搜索结果中返回的字段数。有关文本命令返回的文档详细信息和项目选项,请参阅以下链接。
http://docs.mongodb.org/manual/reference/command/text/

关于java - 如何使用java从mongodb中的完整测试搜索结果中获取特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17363520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com