Reverse search at mongodb - mongodb

I have tried to search around for clues but didn't find any.
Please let me ask a question about reverse search for mongodb.
I have a collection that contain a phone prefix, let say bellow.
{"prefix":"1234"}
and i am trying to make it hit with value like 12341111 or 12342222 ...
in MySQL i can do it with below SQL.
SELECT * from phoneprefix where ‘12341111’ like concat(prefix,’%’)
Is there anyway to make it possible at mongodb ?
Thanks for reading my post and please have a nice day !

The only way I can see to do this is to use the performance-challenged $where operator:
db.phoneprefix.find({$where: function() {
return (new RegExp('^' + this.prefix)).test('12341111');
}});

You could use $regex in MongoDB.

Related

How do you search an array of documents in mongo dart?

How do I search an array of documents in mongodb using the dart programming language?
Im using https://pub.dartlang.org/packages/mongo_dart for the driver
The example db collection looks like this
"name": [
{"full":"Tyler Thompson",
"first":"Tyler",
"last":"Thompson"
}
]
Using the following query will help achieve your goal for the one name
String search = "Tyler";
{"name.full": {'\$regex': '${search}'}
Hope that helps! Couldn't find an example, so I thought I'd give one!:)
You can try to look at https://github.com/vadimtsushko/mongo_dart/blob/master/example/queries.dart
It is plausible that for your sample something like this would work:
coll.find(where.eq('name.full','Tyler'))
or maybe
coll.find(where.match('name.full','^Tyl[eo]r'))

ElasticSearch - filter terms for autocomplete

I would like for an autocomplete to get the terms starting with some specific characters.However, the terms returned do not begin with the specified text ("wer" in this case), and more than that, no matter what the prefix content is, they are always the same. The query I am using now is:
{"facets":
{"count":
{"terms":
{"field":"content"},
"facet_filter":
{"prefix":{"content":"wer"}}
}
},
"size":0
}
I am wondering what am I missing or doing wrong. Any help much appreciated. Thanks!
Perhaps you miss a "query" entry after the facet_filter.
If this does not help try regex pattern for facets, but be aware that facets will be removed in future updates of elasticsearch.
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-terms-facet.html#_regex_patterns

CakePHP, MongoDB: How to search field from Collection

I want to implement functionality that will search the record from mongoDB. I am using ichikaway plugin.
I tried the cakephp like clause mentioned here, but did not worked for me :(.
please let me if there is any approach to achieve this.
Thanks in advance
After doing search around I have found solution
$conditions = array('field_name' => new MongoRegex('/'.$value.'/i'));
where i trans to case-insensitive.

How can we use like in yii Mongodb?

Could any one please tell me the syntax for like in yii mongodb .I would like to search for a particular name using like as in sql from mongo db collection.How can we write the query for this.
You can use the MongoRegex object: http://php.net/manual/en/class.mongoregex.php which is compatible with components like YiiMongoDBSuite.
It uses a regular expression to match so %sammaye% in SQL = new MongoRegex('/sammaye/') in MongoDB.
Edit
To search by sammaye% you can do new MongoRegex('/^sammaye/') as asked here: https://stackoverflow.com/questions/13194639/like-function-with-yii-mongo-db
If you use the ActiveRecord in Yii2, you should choose simple like part in the where condition:
ActiveRecord::findOne(['like', 'field', 'query'])
For more information see related docs: http://www.yiiframework.com/doc-2.0/yii-db-queryinterface.html#where()-detail
or of course there are also available the PHP's MongoDB commands:
$mongo = \Yii::$app->mongodb;
$collection = $mongo->getCollection('your collection name');
$result = $collection->find(['field' => new \MongoDB\BSON\Regex('^stratswith')]);
Hope it helps!
There is no like support. But based on your requirement, you can use the regex support.
Below are the examples from their SQL to MongoDB query mapping chart.
SELECT * FROM users WHERE name LIKE "%Joe%" = db.users.find({name:/Joe/})
SELECT * FROM users WHERE name LIKE "Joe%" = db.users.find({name:/^Joe/})

How to get whole index in ZEND Lucene?

Hi I am looking for a way to get the whole index if my query is nothing.
My lucene is a picture of my database without some unwanted content, like expired annonces...
So I would like to use only lucene to get annonces, and for that I need a way to get the whole index.
Any ideas?
thanks!
This is not the answer but something wich works for me:
Using an indexKey like is_indexed, always true.
I am adding "is_indexed:1" to my query and it works...
If you have something else, let me know!
I tend to use a field which is named after the index such as:
$oDoc->addField(
Zend_Search_Lucene_Field::keyword(
'index',
'availability'
)
);
Then the term query will return all fields. It's not pretty but it works fine.