1

I am trying to return all the records that match my searchtext.So far I have only seen examples where we need to specify field name but I want return records if any of the field contains the searchtext, without specifying any field name. And I got to see $text , but unfortunatly it's not supported in cosmosdb API mongodb.

Can someone please help me to resolve this issue ?

Here is what I tried but failed

let querySpec = {
entity: "project",
$text: { $search: "\"test\"" } ,
$or: [{
accessType: "Private",
userName: userName   
}, {
accessType: "Public"
}]
}
dbHandler.findandOrder(querySpec, sortfilter, "project").then(function (response) {
res.status(200).json({
status: 'success',
data: utils.unescapeList(response),
totalRecords:response.length
});




exports.findandOrder = function (filterObject, sortfilter, collectionname) {
return new Promise((resolve, reject) => {
return getConnection().then((db) => {
if (db == null) {
console.log("db in findandOrder() is undefined");
} else {
db.db(config.mongodb.dbname).collection(collectionname).find(filterObject).sort(sortfilter).toArray((err, res) => {
if (db) {
//db.close();
}
if (err) {
reject(err);
} else {
resolve(res);
}
});
}
});
});
};

Error: {"message":{"ok":0,"code":115,"codeName":"CommandNotSupported","name":"MongoError"}}

I am using $regex as temperory solution as $text is not supported.

Please suggest ...

sonuD
  • 47
  • 9

1 Answers1

0

From the MongoDB manual, Create a Wildcard Index on All Fields:

db.collection.createIndex( { "$**" : 1 } )

but this is far from an ideal way to index your data. On the same page is this admonition:

Wildcard indexes are not designed to replace workload-based index planning.

In other words, know your data/schema and tailor indices accordingly. There are many caveats to wildcard indices so read the manual page linked above.

Alternately you can concatenate all the text you want to search into a single field (while also maintaining the individual fields) and create a text index on that field.

Paul
  • 19,704
  • 14
  • 78
  • 96