Sample Header Ad - 728x90

MongoDB text index and text search

6 votes
2 answers
2899 views
I have a MongoDB collection db.articles,it has 2 keys ,"title" and "abstract".I want to do text search on these two keys.For example, the search text is "physics",and I want all the documents whose "title" or "abstract" contains keyword "physics" is returned.But how to create the text index to meet my command is really confusing me: Should I create two separate text indexes for both of them like this: db.articles.ensureIndex({title:"text"}) db.articles.ensureIndex({abstract:"text"}) or should I create a index in a single command and give the equal weight: db.articles.ensureIndex( { title: "text", abstract: "text", }, { weights: { title: 1, abstract:1, }, name: "TextIndex" } ) I am already get used to the operation find(),whose query granularity is key,that is ,you should indicate the key you want to query on.But for text index, it seems like a document granularity,you cannot indicate the key you want to query on , instead ,you can only indicate the document name.So , what can I do if I want to do text search on a special key?
Asked by wuchang (373 rep)
Mar 11, 2014, 02:27 AM
Last activity: Jul 15, 2025, 10:58 AM