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/})
Related
I'm using mongo's findOneAndReplace() with upsert = true and returnNewDocument = true
as basically a way to not insert duplicate. But I want to get the _id of the new inserted document (or the old existing document) to be passed to a background processing task.
BUT I also want to log if the document was Added-As-New or if a Replacement took place.
I can't see any way to use findOneAndReplace() with these parameters and answer that question.
The only think I can think of is to find, and insert in two different requests which seems a bit counter-productive.
ps. I'm actually using pymongo's find_one_and_replace() but it seems identical to the JS mongo function.
EDIT: edited for clarification.
Is it not possible to use replace_one function ? In java I am able to use repalceOne which returns UpdateResult. That has method for finding if documented updated or not. I see repalce_one in pymongo and it should behave same. Here is doc PyMongo Doc Look for replace_one
The way I'm going to implement it for now (in python):
import pymongo
def find_one_and_replace_log(collection, find_query,
document_data,
log={}):
''' behaves like find_one_or_replace(upsert=True,
return_document=pymongo.ReturnDocument.AFTER)
'''
is_new = False
document = collection.find_one(find_query)
if not document:
# document didn't exist
# log as NEW
is_new = True
new_or_replaced_document = collection.find_one_and_replace(
find_query,
document_data,
upsert=True,
return_document=pymongo.ReturnDocument.AFTER
)
log['new_document'] = is_new
return new_or_replaced_document
I have 1 issue with mongodb query search with exact values. i want to get collections irrespective of case sensitive. for this i found some querys like below. its working fine.
db.applications.find({"blocks.HOSPITAL_INFO.data.name": new RegExp('^VIKRAM$', 'i')});
in laravel i am using jessengers. . above query i can write as raw query in laravel.
but my issue is when ever i am using $In:{'a','b'} like this how can i write regex for this. FYI 'a','b' are dynamic array values. so how can i write regex for these array values?
The query in MongoDB would be something like this:
db.applications.find({"blocks.HOSPITAL_INFO.data.name":
{$in:[new RegExp('^a$', 'i'),new RegExp('^b$', 'i')]}});
OR, alternatively:
db.applications.find({"blocks.HOSPITAL_INFO.data.name":
{$in:[/^a$/i,/^b$/i]}});
...where a & b are your dynamic variables.
I'm not very familiar with Laravel, but I'm guessing it would look something like this:
$applications = Application::whereIn('blocks.HOSPITAL_INFO.data.name',
[new MongoRegex('^a$/i'), new MongoRegex('^b$/i')])->get();
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.
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.
I need to find out distinct values(for example : CreationDate or SourceSystem) in MongoDB using MongoVUE. FYI, I can only use the trial version of the same.
I don't think you can do that using MongoVUE. You can do it through MongoDB shell running a command like this:
db.[Collection Name].distinct({Property Name})
ex: db.students.distinct('age')
db.students.distinct('age').length; // gives you the record count
I usually find SQL to Mongo Mapping Chart page useful in these case ( http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart )
From having a quick look, I can't see how you can either, as the "Find" feature forces you to use:
db.[collectionName].find({...})
so doesn't allow you to do:
db.[collectionName].distinct({...})
I recommend using the normal command line executable for Mongo instead of MongoVUE, then you can use the commands from the 10Gen documentation:
http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct