MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Combining $searchBeta with eg. $match

I am testing searchBeta and I am trying to combine it with $match to add stronger matches that don’t need a lucene index.

Eg, the working lucene search looks like this (I know a compound is not needed here, it is just an example taken from a bigger query):

db.getCollection('col').aggregate([
   {

     "$searchBeta": {
         "compound" : {
             "must" : [
                {
                    "term": {
                        "path": ["firstName","middleName","lastName"],
                        "query": "smit*",
                         "wildcard": true,
                        }
                 }
             ],
         }
     }
   }
])

which gives a nice result containing 3 documents in my case:

ObjectId("5d4c160dd54af60001a3a1dd")
ObjectId("5d4c1678d54af60001a3a1e9")
ObjectId("5e7c6675e57e1100013bcbd2")

Now I am just trying to see if it is possible to combine a search with a stronger match and tried it with one of the above object ids, but this gives an error:

db.getCollection('parties').aggregate([
   {
       "$match" : {
           "_id" : ObjectId("5d4c160dd54af60001a3a1dd")
           
           },
     "$searchBeta": {
         "compound" : {
             "must" : [
                {
                    "term": {
                        "path": ["firstName","middleName","lastName","identityMail"],
                        "query": "eefje*",
                         "wildcard": true,
                        }
                 }
             ],
         }
     }
   }
])

The error is a follows:

Error: command failed: {
	"operationTime" : Timestamp(1587733651, 1),
	"ok" : 0,
	"errmsg" : "A pipeline stage specification object must contain exactly one field.",
	"code" : 40323,
	"codeName" : "Location40323",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1587733651, 1),
		"signature" : {
			"hash" : BinData(0,"1jukCsxHk+0Z4Je7FwZwLs3csr4="),
			"keyId" : NumberLong("6764005400170725379")
		}
	}
} : aggregate failed 

Shouldn’t this just work?

regards,

Sven

Found it, first I made the mistake of positioning the $match, it shouldn’t have been next to $searchBeta of course, but a level up as an element in the array.

Secondly, once I tried that I got an error message that made more sense and had to move the $match after the $searchBeta in the array.

Now it works! Cool stuff!

1 Like