1

I want to filter out not in array documents using atlas search(like $nin operation on an array of objectId). Adding where cause slows down my query. Can someone suggest to me how to do it? My query is as below.

aggregate(
    [
        { 
            "$search" : { 
                "index" : "textsearch", 
                "compound" : { 
                    "must" : [
                        { 
                            "text" : { 
                                "query" : "mission", 
                                "path" : "content"
                            }
                        }, 
                        { 
                            "near" : { 
                                "path" : "createdAt", 
                                "origin" : ISODate("2022-03-21T00:00:00.000+0000"), 
                                "pivot" : NumberLong(7776000000)
                            }
                        }
                    ], 
                    "mustNot" : [
                        { 
                            "text" : { 
                                "query" : [
                                    ObjectId("5fecb2baa431cc44e6248dd2"), 
                                    ObjectId("5fe4c99a8117ee74c8478e66")
                                ], 
                                "path" : "uid"
                            }
                        }
                    ]
                }
            }
        }, 
        { 
            "$project" : { 
                "_id" : 0.0, 
                "createdAt" : 1.0, 
                "content" : 1.0, 
                "uid" : 1.0, 
                "score" : { 
                    "$meta" : "searchScore"
                }, 
                "type" : 1.0
            }
        }
    ])

1 Answers1

0

You should use the filter search operator when trying to exclude specific results from a mongodb search query. Operators must should must not all impact scoring of the result set, whereas filter will not and you can explicitly exclude results like $nin. Future reference here is the official documentation.

I don't have you index definition but something like this would work.

aggregate(
    [
        { 
            "$search" : { 
                "index" : "textsearch", 
                "compound" : { 
                    "must" : [
                        { 
                            "text" : { 
                                "query" : "mission", 
                                "path" : "content"
                            }
                        }, 
                        { 
                            "near" : { 
                                "path" : "createdAt", 
                                "origin" : ISODate("2022-03-21T00:00:00.000+0000"), 
                                "pivot" : NumberLong(7776000000)
                            }
                        }
                    ], 
                    "filter" : [
                        { 
                            "text" : { 
                                "query" : [
                                    ObjectId("5fecb2baa431cc44e6248dd2"), 
                                    ObjectId("5fe4c99a8117ee74c8478e66")
                                ], 
                                "path" : "uid"
                            }
                        }
                    ]
                }
            }
        }, 
        { 
            "$project" : { 
                "_id" : 0, 
                "createdAt" : 1, 
                "content" : 1, 
                "uid" : 1, 
                "score" : { 
                    "$meta" : "searchScore"
                }, 
                "type" : 1
            }
        }
    ])
Scott
  • 174
  • 1
  • 13