2013-07-04 62 views
1

大家好,我Mongo的數據庫字段如下:如何在mongodb數組字段上進行全文搜索?

 "_id" : ObjectId("51d582be7a8d51bd29ca78a3"), 
     "Filename" : "sample.pdfe", 
     "Title" : null, 
     "Author" : null, 
     "ContentType" : "application/x-msdownload; format=pe32", 
     "url" : "Z:sample.pdf", 
     "Keywords" : ["php","java",".net","python"] 
下面

是我的代碼做全文搜索的索引字段

m = new Mongo("10.0.0.26", 27017); 
DB db = m.getDB("soft") ; 
DBCollection col = db.getCollection("metadatanew") ; 
String collection = col.toString(); 
DBObject searchCmd = new BasicDBObject(); 
searchCmd.put("text", collection); 
searchCmd.put("search", name); 

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

我用下面的命令在兩個字段創建文本索引:

db.metadatanew.ensureIndex({'Filename':"text"}, {'Keywords':"text"}) 

我已經在文件名和關鍵字上創建了文本索引,但是我可以只對文件名進行全文搜索,在我的代碼中是否有任何錯誤, ase幫我

回答

2

另一種方法是在這兩個領域創建唯一索引:

BasicDBObject basicDBObject = new BasicDBObject(); 
basicDBObject.append("Keywords", "text"); 
basicDBObject.append("Filename", "text"); 
dbCollection.createIndex(basicDBObject); 
相關問題