Case insensitive search in mongodb and nodejs inside an array

1.6k views Asked by At

I want to perform a tag search which has to be case insensitive against tag keywords. I need this for a single keyword search and how to do that for multiple keywords too. But the problem is when I search with following queries I am getting nothing. I am new to NodeJs and MongoDb so if there is any mistake in the queries please do rectify me.

The tags can be 'tag1' or 'TAG1' or 'taG1'.

for single tag keyword search I have used (I'm not getting any result):

db.somecollection.find({'Tags':{'TagText': new RegExp('Tag5',"i")}, 'Status':'active'})

for multiple tag keyword search (need to make this case insensitive too :( )

db.somecollection.find({'Tags':{'TagText': {"$in": ['Tag3','Tag5', 'Tag16']}}, 'Status':'active'})

the record-set in the db:

{
    "results": {
        "products": [
            {
                "_id": "5858cc242dadb72409000029",
                "Permalink": "some-permalink-1",
                "Tags": [
                        {"TagText":"Tag1"},
                        {"TagText":"Tag2"},
                        {"TagText":"Tag3"},
                        {"TagText":"Tag4"},
                        {"TagText":"Tag5"}
                    ],
                "Viewcount": 3791
            },
            {
                "_id": "58523cc212dadb72409000029",
                "Permalink": "some-permalink-2",
                "Tags": [
                        {"TagText":"Tag8"},
                        {"TagText":"Tag2"},
                        {"TagText":"Tag1"},
                        {"TagText":"Tag7"},
                        {"TagText":"Tag2"}
                    ],
                "Viewcount": 1003
            },
            {
                "_id": "5858cc242dadb11839084523",
                "Permalink": "some-permalink-3",
                "Tags": [
                        {"TagText":"Tag11"},
                        {"TagText":"Tag3"},
                        {"TagText":"Tag1"},
                        {"TagText":"Tag6"},
                        {"TagText":"Tag18"}
                    ],
                "Viewcount": 2608
            },
            {
                "_id": "5850cc242dadb11009000029",
                "Permalink": "some-permalink-4",
                "Tags": [
                        {"TagText":"Tag14"},
                        {"TagText":"Tag12"},
                        {"TagText":"Tag4"},
                        {"TagText":"Tag5"},
                        {"TagText":"Tag7"}
                    ],
                "Viewcount": 6202
            },

        ],
        "count": 4
    }
}
1

There are 1 answers

2
s7vr On BEST ANSWER

Create a text index for the field that you want search on. (Default is case insensitive)

db.somecollection.createIndex( { "Tags.TagText": "text" } )

For more options, https://docs.mongodb.com/v3.2/core/index-text/#index-feature-text

Make use $text operator in combination with $search for searching the content.

For more options, https://docs.mongodb.com/v3.2/reference/operator/query/text/#op._S_text

Search with single term

db.somecollection.find({$text: { $search: "Tag3"}});

Search with multiple search terms

db.somecollection.find({$text: { $search: "Tag3 Tag5 Tag16"}});

Update:

Looks like you are looking for case insensitive equality which can be easily achieved by regex. You'll not need text search. Drop the text search index.

Search with single term

db.somecollection.find({'Tags.TagText': {$regex: /^Tag3$/i}}).pretty();

Search with multiple search terms

db.somecollection.find({'Tags.TagText': {$in: [/^Tag11$/i, /^Tag6$/i]}}).pretty();