Home > Blockchain >  Searching by text, using mongoDB aggregation
Searching by text, using mongoDB aggregation

Time:03-14

I am trying to do a text search of the collection called DAFacility in MongoDB Compass:

_id:62170597b3fa8994a0d9a0c8
author:"User"
organizationName:"TSTT"
eventName:"Facility Assessment After Disaster"
eventDate:2022-02-01T00:00:00.000 00:00
area:"Siparia"
disasterNature:"Earthquake"
threatLevel:"High"
surroundingDamage:"Cracked Foundations and Roads"
facilityName:"Point Lisas Main Facility"
facLocation:Array
facStatus:"Operable"
operEqu:23
inoperEqu:7
facilityDamage:"Cracked Walls and Floors"
facImage:Array
__v:0

I am trying to search the field facilityDamage where i can search maybe one word from the entire data entry (eg searching the word "Walls" and having the entire entry shows up)

I am trying to perform it within mongoDB data aggregation option with the template being:

[
    {
        '$search': {
            'index': 'string', 
            'text': {
                'query': 'string', 
                'path': 'string'
            }
        }
    }
]

I have read the document which got me more confused as to what goes in the index, query and path. can you all advise me as to what variables goes into index, query and path.

Whenever i use it within node it returns an empty array:

exports.DAEquipment_damage_search = (req, res) => {
  DAEquipment.aggregate([
    [
      {
        '$match': {
        '$or': [
           {'facilityDamage':{ '$regex':'.*'   req.body.facilityDamage   '.*','$options': 'i' } }
        ]
    }
    }
    }
  ]
  ]).then((DAEquipment) => {
    res.send(DAEquipment);
    console.log(DAEquipment);
  })
  .catch((e) => {
    res.send(e);
  });
};

CodePudding user response:

await DAFacility.aggregate([
{
    $match: {
        $or: [
           {"facilityDamage":{ $regex:'.*'   searchText   '.*',$options: 'i' } },
        ]
    }
},
])

May it will help you

  • Related