"LIKE" command in MongoDB(mongomapper) - mongodb

how to use filter data as sql do "LIKE" in MongoDB, instead I using gem mongomapper on my rails apps? .thanks

If you're looking for partial matches on a string you can query with a regular expression. Here's the relevant part of the mongomapper docs:
http://api.mongodb.org/ruby/current/file.TUTORIAL.html#Querying_with_Regular_Expressions
Worth noting this from the Mongodb docs:
"For simple prefix queries (also called rooted regexps) like /^prefix/, the database will use an index when available and appropriate (much like most SQL databases that use indexes for a LIKE 'prefix%' expression). This only works if you don't have i (case-insensitivity) in the flags."

the closest thing to SQL LIKE would be /query/
ex:-
Person.where('name' => /John/).all => John F, John Doe, Johnny...etc
Edit: this is still case sensitive

try these it work for me :
#store_array=User.where(:$or => [{:first_name => /.#{#search_text}./i}, {:last_name => /.#{#search_text}./i}]).all();

Related

TYPO3 queryBuilder: How to work with BINARY in where() clause?

I just have a short question.
There's no description in the following API overview of TYPO3 how to use a "BINARY" in where() clause: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Database/QueryBuilder/Index.html#expr
What I want to achieve? this one:
WEHRE BINARY `buyer_code` = "f#F67d";
Actually I can only do the following:
->where(
$queryBuilder->expr()->eq('buyer_code', 'f#F67d')
);
But in this case I don't get a satisfying result for myself because I need case-sensitive here :-)
An another buyer_code exists "f#F67D" (the last char is uppercase) but I do need to look for the other one.
Thanks for helping.
Since TYPO3 is using Doctrine API here, you could try to do
->where('BINARY `buyer_code` = ' . $queryBuilder->createNamedParameter('f#F67d'))
Please keep in mind, that this query now only works for database backends, supporting the BINARY keyword!
Please have a look at Doctrine2 case-sensitive query The thread is a bit older, but seems to cover background and solution for your problem.

LINQ query where string does not contain a number

New to LINQ and I'm having trouble coming up with a simpler way to add a condition to exclude fields that contain numbers.
So far I've come up with:
db.MyDB.Where(x => !x.MyField.Contains('1') && !x.MyField.Contains('2') && !x.MyField.Contains('3') &&... etc.
There must be a better way to achieve this. I'm using entity framework.
Try something like this:
db.MyDB.Where(x => !x.MyField.Any(x => Char.IsDigit(x)))
You could also consider using a regex with \d.
There are a number of solutions, I suppose it comes down to what works best with whatever MyDB is in this scenario. You could make it more sql friendly by playing with SqlMethods
db.MyDB.Where(x => !SqlMethods.Like(x.MyField, "[0-9]"))
If this is linq to sql, use SqlMethods.Like
db.MyDB.Where(x => SqlMethods.Like(x.MyField, "%[0-9]%");
[edit] Well, it's not L2S, it's EF. I believe EF supports PatIndex
db.MyDB.Where(x => SqlFunctions.PatIndex("%[0-9]%", x.MyField) > 0);
(PATINDEX is 1-based, not 0 based)

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>

MongoDB database design with embedded documents

I have a few thousand strings (items) that I would like to translate. I have structured my MongoDB as follows:
#document = {:item => "hello", :translations => {:fr => {:name => "bonjour",
:note => "easy"}, :es => {:name => "hola", :note => "facil"}}}
The :translations field can contain many more languages and properties. I would like to run queries such as retrieving all items with no translations for a specific language, or retrieving all items having 'bonjour' as a French translation.
I don't see how I can do this. Is there a better way to structure my database for these purposes? I am using node.js.
Thanks.
I would like to run queries such as retrieving all items with no translations for a specific language,
.find({ 'translations.fr': {$exists:false} })
...or retrieving all items having 'bonjour' as a French translation.
.find({ 'translations.fr.name': "bonjour" })
Is there a better way to structure my database for these purposes?
I believe that you have the correct structure. You will have to become familiar with the Dot Notation.
I'd say that for your purpose the model is good. You need mongo dot notation, you can use $exists to look for fr and dot notation for bonjour -
find({ "fr.name" : "bonjour" })

MongoDB dbref in perl

Is there a way to do dbrefs using the Perl API? Its not here nor is it anywhere here.
Here s a sample schema:
book: name, publisher,isdn,{author}
author : name,date of birth
I could just add a field to serve as the reference but wanted to do it with dbref instead.
There are no helpers for it, yet. DBRefs are just normal hashes, though, so you can access/create them yourself. They have the form:
my $ref = {'$ref' => $collection_name, '$id' => $id};
See http://www.mongodb.org/display/DOCS/DB+Ref for more info.
It's also handled by MongoDBx::Class, but this is a fully fledged ORM so it may not fit your use case.
https://metacpan.org/pod/MongoDBx::Class
Its handle by MongoDBx::AutoDeref.
https://metacpan.org/pod/MongoDBx::AutoDeref