MongoDB + Java + Search to fetch field or column names only after searching a string in a document using either full text or regular search - mongodb

I am am in need of a solution quickly for the following:
Assuming that I have n numbere of documents in collection XYZ similar to below:
XYZ{
NAME: Test
AGE: 33
ADDRESS: ASDF
EDUCATION: BSC
}
If I want to search for a string say: "BSC"
I want the api to search for the string "BSC", and after successful search, it has to return me only the column name( or field name) of the document XYZ: "EDUCATION"
Any quick help for the solution of the above problem would be very helpful.
Thanks.

MongoDB does not allow you full text search, Sorry, I am not able to comment this one, Don't have appropriate points.

Related

How to check if list of string contains (substring) value in EpiFind?

I have indexed an object which has a list of strings like ["ZZA-KL-2A", "ZZA-KL-ZZB"]. I want to search and get all items which starts with a certain 3 letter code. So I want to check each item in the list and check something like 'StartsWith'.
I can see from documentation that we have something like Match, MatchContained but nothing for start with for the list of string items.
Please note that this question is not related to ordinary string comparison in C# or LINQ before flagging the question.
Just use a filter
var searchQuery = client.Search<MyContent>()
.Filter(x => x.OrderNumber.StartsWith("Find"));
https://world.episerver.com/documentation/developer-guides/search-navigation/NET-Client-API/searching/Filtering/
You may use Prefix or PrefixCaseInsensitive:
Matching by beginning of a string (startsWith)
The Prefix method lets you match by the beginning of a string. The
following search matches blog posts titled Find and Find rocks! but
not find, Finding or Hello Find.
var searchQuery = client.Search<BlogPost>().Filter(x =>
x.Title.Prefix("Find"));
Use the PrefixCaseInsensitive method to match by the beginning of a
string in a case-insensitive way. The following search matches blog
posts titled Find, Find rocks! and Find but not Finding or Hello Find.
var searchQuery = client.Search<BlogPost>().Filter(x =>
x.Title.PrefixCaseInsensitive("Find"));
Source: https://docs.developers.optimizely.com/digital-experience-platform/v1.1.0-search-and-navigation/docs/strings#matching-by-beginning-of-a-string-startswith

MongoDB query to find documents whose field value is contained in a given string

I have a string, say "string", and a field in a collection, say "name".
I would like to find all the documents whose name is a substring of "string" (it would return for example documents whose name is "str").
I have looked on internet, and I only find the reciprocal, that is for example documents whose name is "string2".
Would someone know please ?
KR
Zlotz
There is no arbitrary substring match operator in MongDB that I know of, but you could try expressing your condition with $where or $regex.
Recently I wanted to achieve the same in my project query below worked for me
db.find({name:{ $regex: new RegExp("^" + "your string pattern".toLowerCase(), "i") } })
Let me know if you find it useful.

MongoDB Text Search find similar words

we have a database with names of wines and beer(s). We're using MongoDB and our document structure for each wine or beer company looks like this:
{
name: String //this is the company name
products: [
{
name: String //this is the product's name
...
}
],
...
}
I have created a text index on both the company name and product name, the text search is working fine but it's not working as expected. For example we'd like our app to be able to return results when someone searches for a product named Bellissima and the text they send is Belisima OR Bellisima or any variations of that word.
Is that even possible with MongoDB's text search? How can I make it smarter? I'm not a MongoDB expert but I know it has something to do with word stemming or something so is there a way I can modify that?? Or any other workaround to give the user most appropriate results? Because searching for Belisima doesn't return any results because that's not the exact word saved in database.

how to find partial search in Mongodb?

How to find partial search?
Now Im trying to find
db.content.find({$text: {$search: "Customer london"}})
It finds all records matching customer, and all records matching london.
If I am searching for a part of a word for example lond or custom
db.content.find({$text: {$search: "lond"}})
It returns an empty result. How can I modify the query to get the same result like when I am searching for london?
You can use regex to get around with it (https://docs.mongodb.com/manual/reference/operator/query/regex/). However, it will work for following :
if you have word Cooking, following queries may give you result
cooking(exact matching)
coo(part of the word)
cooked(The word containing the english root of the document word, where cook is the root word from which cooking or cooked are derived)
If you would like to go one step further and get a result document containing cooking when you type vooking (missplled V instead of C), go for elasticsearch.
Elasticsearch is easy to setup, has extremely powerful edge-ngram analyzer which converts each words into smaller weightage words. Hence when you misspell, you will still get a document based on score elasticsearch gives to that document.
You can read about it here : https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-edgengram-tokenizer.html
it will always return the empty array for partial words like when you are searching for lond to get this type of text london..
Because it take full words and search same as that they are ..
Not achive same results like :-
LO LON LOND LONDO LONDON
Here you may get help from ELASTIC-SEARCH . It is quite good for full text search when implement with mongoDB.
Refrence : ElasticSearch
Thanks
The find all is to an Array
clientDB.collection('details').find({}).toArray().then((docs) =>
I now used the str.StartWith in a for loop to pick out my record.
if (docs[i].name.startsWith('U', 0)) {
return console.log(docs[i].name);
} else {
console.log('Record not found!!!')
};
This may not be efficient, but it works for now

Mongoid, find object by searching by part of the Id?

I want to be able to search for my objects by searching for the last 4 characters of the id. How can I do that?
Book.where(_id: params[:q])
Where the param would be something like a3f4, and in this case the actual id for the object that I want to be found would be:
bc313c1f5053b66121a8a3f4
Notice the last for characters are what we searched for. How can I search for just "part" of my objects id? instead of having my user search manually by typing in the entire id?
I found in MongoDB's help docs, that I can provide a regex:
db.x.find({someId : {$regex : "123\\[456\\]"}}) // use "\\" to escape
Is there a way for me to search using the regular mongo ruby driver and not using Mongoid?
Usually, in Mongoid you can search with a regexp like you normally would with a string in your call to where() ie:
Book.where(:title => /^Alice/) # returns all books with titles starting with 'Alice'
However this doesn't work in your case, because the _id field is not stored as a string, but as an ObjectID. However, you could add (and index) a field on your models which could provide this functionality for you, which you can populate in an after_create callback.
<shameless_plug>
Alternatively, if you're just looking for a shorter solution to the default Mongoid IDs, I could suggest something like mongoid_token which makes it pretty easy to add shorter tokens/ids to your Mongoid documents.
</shameless_plug>