4

I have a sails-mongodb query which is

db('insights').find( { $text: { $search: search} } ).limit(limit).skip(offset).exec(function (err, insights) { ... }

which give an error of

{"name":"MongoError","message":"\"$search\" had the wrong type. Expected String, found RegEx","waitedMS":0,"ok":0

The equivalent Mongodb query works.

var col = db.collection('insights');

          col.find( { $text: { $search: search } } ).skip(offset).limit(limit).toArray(function(err, items) { .. }

An example value of 'search' is "\"jim\"" where the search term would be { $text: { $search: "\"jim\" }

What is the correct way to pass in a $text : $search object through sails-mongodb?

Keith John Hutchison
  • 4,955
  • 11
  • 46
  • 64

3 Answers3

1

here is one example you can use 'contains' which can be done using sails-mongo

var musicCourses = await Course.find({ subject: { contains: 'abc' } });

Abhi Patel
  • 214
  • 1
  • 6
1

The answer provided does not work. Sails-MongoDB does not support the MongoDB built in search construct from MongoDB. You're better off using the native Mongo manager from the parent Sails Database then use the native Mongo query like you have in the description. (I know you've moved on, but in case anyone else is looking for the same thing).

const db = sails.getDatastore().manager;
let result = await db.collection("insights").find( { $text: { $search: search } } ).skip(offset).limit(limit).toArray();
nopuck4you
  • 1,730
  • 14
  • 20
1

Here is how I have done it in sails 1.0:

const db = <modelName>.getDatastore().manager;
let rawCollection = db.collection(<modelname>.tableName);
let regex = new RegExp(query, "ig"); //Case insensitive + global

let result = await rawCollection
  .find(
    {
      search: regex,
    }).toArray();
Xfox
  • 174
  • 2
  • 10