flimflam57
flimflam57

Reputation: 1334

Text search in meteor

I'm trying to do something that should be easy, but I think I'm misunderstanding the documentation. In Meteor I'm searching for a particular text string and if it's found then return that document.

So the collection might have the following structure:

{ 
  ...
  DONATE: "15,16,17",
  Amount: "3000,2000,1000",
  Address: "123 Main Street",
  ...
}

Then, if the DONATE string contains "15", for example, then return that document. Something like:

Demographic.findOne({ DONATE: { text: "15"}});

I've read this, but I'm not sure if it's is related.

Upvotes: 2

Views: 825

Answers (1)

Richard Hamilton
Richard Hamilton

Reputation: 26444

MongoDB has a $regex operator

https://docs.mongodb.org/manual/reference/operator/query/regex/

Demographic.findOne({ DONATE: { $regex: /15/ } });

If it finds a match, it will return a mongo object like this

{"_id": ObjectId("jsdkflsdjfklsdfjklsdf", "DONATE": "15,16,16")}

Otherwise it will return null

Tested it out in my console and it works!

Upvotes: 1

Related Questions