As now i have searched and got is i have to specify field names but what i have to do is without specifying field name.. and also i have to save json file data in mongo and from this data i have to partial search. how it can be done?
Asked
Active
Viewed 179 times
1 Answers
0
There's a few steps to this.
First, you want to turn on full text search for the collection in question. You will most likely do this as an admin step on your mongodb CLI:
db.myCollection.ensureIndex(
{ "$**": "text" },
{ name: "TextIndex" }
)
Then you can query that collection for matches anywhere in the text of any document with:
db.myCollection.find( { $text: { $search: "whatever text" } } )
Pushing a JSON file into the DB is pretty straightforward as well. Again from the mongo CLI:
use myDBName
var file = cat('path/to/yourFile.json'); // read the file
var data = JSON.parse(file)
db.myCollection.insert(data);
Doing these things in your node app is fairly similar, though I would generally recommend the CLI approach for the index setup.

Paul
- 35,689
- 11
- 93
- 122