Setting up a keyword query [duplicate] - mongodb

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Full-text search in NoSQL databases
I am somewhat new to database querying and i was wondering what the best way to do this would be. I have a database of articles and want my users to be able to search them by keywords in the tittle, i.e allowing them to type a string and all the tittles containing this string would be selected by the query.
What would the most efficient way to do this be?
And if i want to avoid strings such as "the" or "it" from being selected?
I am using mongoid in case that helps.
Thanks in advance

If your title is stored as a string you could use the regular expression search supported by mongodb. For example:
db.articles.find( { title : /acme.*corp/i } );
Mongodb use PCRE for regular expression. To exclude certain words from the search I would recommend an application side check or you can use the $nin operator. For more info have a look here.

Related

How to check if firestore document field CONTAINS a string [duplicate]

This question already has answers here:
How to search text in Flutter using Firestore
(3 answers)
Google Firestore: Query on substring of a property value (text search)
(25 answers)
Closed 6 months ago.
Basically, I want to use UISearchBar to filter out documents in a collection in a tableview. When the user finishes putting text inside the search bar, the code should fire a query to get the documents only if it contains given text.
For example, User searches: "Th"
What could output in the uitableview:
this
the
them
one thing
breath
This should work no matter where "Th" is within the document field, however I am unsure how to implement this. Like e.g. should I use .whereField(field, isGreaterThan: searchedText)? I don't really know.
Cloud Firestore currently doesn't support native indexing or search for text fields in documents. To enable full text search of your Cloud Firestore data, use a dedicated third-party search service such as Algolia
However, As mentioned in similar thread it supports two query operators for searching text fields which starts substring
I.e. Using isEqualTo, isGreaterThanOrEqualTo and isLessThanOrEqualTo
For example, to search for all documents where name starts with “Th”, you'd use collectionRef.where("name", isGreaterThanOrEqualTo: "Th").where("name", isLessThanOrEqualTo: "Th\uf7ff"). The \uf7ff here is just the last known Unicode character, so that the query stops returning results after dealing with every “Th”
Note: There is no way to search for documents which contain a substring, nor for those whose text ends with a substring.
For other alternative solution you can refer this link

How to perform a sorting with case insensitive in the sails js framework? [duplicate]

This question already has answers here:
Case insensitive sorting in MongoDB
(10 answers)
Closed 7 years ago.
model.find({city: city, sort: 'cityname ASC'}, function(err, cityList){});
the above code giving the correct sorting list but whenever the city is in both uppercase or lowercase like Coimbatore,bangalore,Chennai it gives the output as Chennai,Coimbatore,bangalore but i need the output as bangalore,Chennai, Coimbatore like that. Please help me to get the output as i wanted.. and advace thanking to you all..
This is an error in MongoDB - AFAIk there isn't really a solution for it as of now
similar SO question: Mongo DB sorting With case insensitive
mongoDB JIRA issue: https://jira.mongodb.org/browse/SERVER-90
Update: A workaround I've found - store the strings a second time only upper or only lower case and sort by that property: http://futurestud.io/blog/case-insensitive-sorting-with-mongoose-and-mongodb/

Find documents where any field matches a search string [duplicate]

This question already has answers here:
MongoDB Query Help - query on values of any key in a sub-object
(3 answers)
Closed 8 years ago.
I'm asking this question after studying MongoDB text search and keyword indexes. I also found a similar question, but adding an array to each document containing all the field values seems like a kludgy solution. Thus this question.
We need to write a query that will return documents where any field matches a search string.
Our first thought was to write a query along these lines:
db.collection.find( { field_one: /search string/i,
field_two: /search string/i,
field_three: /search string/i } );
Our challenge is that the fields will vary between documents, so we can't hard-code the field names in the query.
Is something like the following pseudocode possible with MongoDB?
db.collection.find( { [any field]: /search string/i } );
Maybe it's not exactly what you are looking for, but if you are considering a schema restructure that allows such a query, this post appears to be interesting:
MongoDB Query Help - query on values of any key in a sub-object

How do I make a mongodb find where a field(object) includes all properties from my query (names and values)? [duplicate]

This question already has answers here:
MongoDB - how to query for a nested item inside a collection?
(3 answers)
Closed 9 years ago.
Consider my query to be: {cheese:"Cheddar"} and I have the following collections:
{vegetable:"Lettuce", cheese:"Cheddar"}, {cheese:"Blue"}, {milk:"Chocolate}, {cheese:"Cheddar"}
How do I make a find that returns me all collections that include cheese:Cheddar?
The result would be {vegetable:"Lettuce", cheese:"Cheddar"}, {cheese:"Cheddar"} but right now it fives me just {cheese:"Cheddar"}. From what I investigated I only found tokens to work with arrays.
I do NOT know the name of the property is cheese, nor do I know if there are any other ingredients.
I am looking for a way to get documents from a collection, where the query is included in a field, by the names of the properties in the query and the respective values.
Using db.collection.findOne({cheese:"Cheddar"}) you will get as a result only one document, maybe {cheese:"Cheddar"} or maybe {vegetable:"Lettuce", cheese:"Cheddar"}, the first one that MongoDB finds depending on the _id field. If what you want is getting both, you should use db.collection.find({cheese:"Cheddar"}).

'SQL 'like' statement in mongodb [duplicate]

This question already has answers here:
How to query MongoDB with "like"
(45 answers)
Closed 7 years ago.
Is there in MongoDB/mongoose 'like' statement such as in SQL language?
The single reason of using is a implementation of full-text searching.
MongoDB supports RegularExpressions.
Two ways we can implement regular expression using java API for mongodb:
Expression 1:
I need to search start with that string in document field
String strpattern ="your regular expression";
Pattern p = Pattern.compile(strpattern);
BasicDBObject obj = new BasicDBObject()
obj.put("key",p);
Example : (consider I want to search wherever name start with 'a')
String strpattern ="^a";
Pattern p = Pattern.compile(strpattern);
BasicDBObject obj = new BasicDBObject()
obj.put("name",p);
Expression 2:
I need to search multiple words in one field , you can use as follows
String pattern = "\\b(one|how many)\\b";
BasicDBObject obj = new BasicDBObject();
//if you want to check case sensitive
obj.put("message", Pattern.compile(pattern,Pattern.CASE_INSENSITIVE));
Although you can use regular expressions, you won't get good performance on full text search because a contains query such as /text/ cannot use an index.
A begins-with query such as /^text/ can, however, use an index.
If you are considering full text search on any large scale please consider MongoDB's Multi Key Search feature.
Update
As of MongoDB v3.2 you can also use a text index.
Consider making a script at application level that transforms your data to tokens (words). Then treat tokens as tags, build an index on those tokens, and then search the tokens like searching for tags. This is like creating an inverted index.
For way better search capabilities on text consider using Lucene instead of MongoDB.
Use regex: /^textforesarc$/i. Just don't forget to say goodbye to performance :).
Simulating regex search with like will not hit custom defined index. Only starts with is supported for now.