how to do multiple where with same field? - mongodb

How to convert this script for mongoDB?
select * from table where id > -1 and id in (4,5)
I need to execute this query with two where conditions on the same field.
This doesn't seem to work in PHP:
$db->$col->find(
array(
'id' => array( '$gt' => -1 ),
'id' => array( '$in' => array( 4, 5 ) )
)
);

Basically the problem is that you have a duplicate key in your array so the latter overwrites the prior so your query is only doing the $in.
Most poeople look to $and to solve this problem however, very few know that actually:
'id'=>array('$gt'=>-1, '$in'=>array(4,5))
Will also solve it. You can just chain your constraints for that field.

Related

In Laravel Backpack - Column data in view details and XLS download is truncated

If the column data has approximately more than 50 characters the column gets truncated at around 50.
addColumn function has 'type' => 'text'
In the end there is [...]
This is both in the details pop-up and XLS download.
Question is, can this limit be increased from any configuration?
Yes, you can! There is a limit option on the column for that.
$this->crud->addColumns([
[
'name' => 'name',
'label' => 'Name',
'type' => 'text',
'limit' => 150,
],
...
]);

TYPO3 TCA select with own items

I use TYPO3 version 9.
I am trying to configure TCA selectMultipleSideBySide. I want to use without foreign table, I want to pass my own items. It displays correctly, but when I try to add more then 1 item, I get error:
These fields of record 3 in table "tx_scout24_domain_model_vehicle" have not been saved correctly: equipment! The values might have changed due to type casting of the database.
It because TYPO3 try to save data to main table, but not in mm table.
My current TCA:
'equipment' => array(
'label' => 'LLL:EXT:scout24/Resources/Private/Language/locallang_db.xlf:equipment',
'config' => array(
'type' => 'select',
'renderType' => 'selectMultipleSideBySide',
'items' => \Istar\Scout24\Service\FieldService::getFields('equipment'),
'MM' => 'tx_scout24_vehicle_equipment_mm',
),
),
According to the documentation the values are stored as comma-separated values. It is not possible to use a MM relation table. To store the values the columns must be of type varchar. Because you can store one value it seems that the type of your columns is an int, which can be stored without a problem.
So you have to do:
Remove the MM relation in your configuration
Change your table column to varchar

MongoDB-PHP: How to sort the result of find query by timestamp in ascending/descending order?

I want to sort the result of find query in ascending/descending order w.r.t timestamp(time_created).
My query is:
$mongoResult = $mongoDb->find(array('organization_id' => new MongoId($_SESSION['user_id'])));
Try:
$mongoResult = $mongoDb->find(
array(
'organization_id' => new MongoId($_SESSION['user_id'])
))->sort(array("time_created" => -1)
);
You can use the _id field to sort by timestamp.
The following query can print the latest 100 records.
$mongoResult = $mongoDb->find()->sort( array("_id" => -1 ))->limit(100);

How to optimize this mongo query?

I have a simple mongo query here which uses aggregate function. It is taking 1 min and 38 sec to load 10 rows in my php script. How can I optimize this query as it takes lesser time.
$data = $mongo->command(
array(
'aggregate' =>"my_collection",
'pipeline' => array(
array('$match' => $filter_query),
array('$group' => array('_id'=>'$email')),
array('$skip'=>$offset),
array('$limit'=>10)
)
),
array( 'timeout' => "-1")
);
This is my query and the filter_query is another array which is
Array ( [event] => spamreport [timestamp] => Array ( [$gt] => 1384236000 [$lt] => 1384840800 ) )
Also I have one more query to find the distinct values like this
$distinct_notifications = $my_collection->distinct('email',$filter_query);
It is taking forever to load the distinct values. My collection is little larger and has around 20700144 rows. It is increasing day by day. Also it will be helpful if someone can point out a good map/reduce tutorial as I am new to this.

Proper indexing and sorting with MongoDB

I have a relatively complex query that I'm running. I've included it below, but essentially I have a location query combined with an $or and $in query all to be sorted by date/time. Whenever I run the full query though it returns in ascending order rather than descending.
If I pull out the location part of the query, it successfully returns the results in the proper order, but once I put the location component of the query back in, it continues to fail.
Here's the query as written in php:
$query = array(
'$or' => array(
array( ISPUBLIC => true ),
array( GROUP_INVITES.".".TO.".".INVITE_VAL => array( '$in' => $user_groups ) )
),
LOC => array( '$near' => array( (float)$lon , (float)$lat ), '$maxDistance' => 1 ) //Using 1 degree for the max distance for now
);
$res = $this->db->find( $query )->sort(array('ts'=>-1))->limit($limit)->skip($start);
The fields used are constants that map to fields in the database. My first assumption was that the indexes are not configured properly, but how should I have this indexed when combining geospatial queries with others?
UPDATE
I've been able to replicate the problem by simplifying the query down to:
$query = array(
HOLLER_GROUP_INVITES.".".HOLLER_TO.".".INVITE_VAL => array( '$in' => $user_groups ),
HOLLER_LOC => array( '$near' => array( (float)$lon , (float)$lat ), '$maxDistance' => 1 )
);
UPDATE 2
I've run additional tests and it appears that when limit is removed it's sorted in the correct direction. No idea what's going on here.
This question appears to have the answer: Sorting MongoDB GeoNear results by something other than distance? .... apparently $near was already performing a sort. If you simply want to limit within a given boundary but sort by something else, you should use $within instead of $near.