New to Mongo here. I'm using the PHP lib and trying to work out how I can find in a collection from multiple queries. I could do this by repeating the query with a different query, but I wondered if it can be done in one.
I.e.
$idsToLookFor = array(2124,4241,5553);
$query = $db->thisCollection->find(array('id' => $idsToLookFor));
That's what I'd like to do. However it doesn't work. What I'm trying to do is find a set of results for all the id's at one time.
Possible or just do a findOne on each with a foreach/for?
Use $in Operator
$idsToLookFor = array(2124,4241,5553);
$query = $db->thisCollection->find(array(
'id' => array('$in' => $idsToLookFor)
));
Related
I'm retrieving some traffic data of a website using "scan" option in Dynamodb. I have used filterExpression to filter those out.
I will be doing scanning against a large table which will have more than 20GB of data.
I found that DynamoDB scans throguh the entire table and filter the results out. The document says it only returns 1MB of data and then i have to loop through again to get the rest. It seems to be bad way to make this work.
got the reference from here: Dynamodb filter expression not returning all results
For a small table that should be fine.
MySQL dose the same I guess. I'm not sure.
Which is faster to read is it MySQL select or DynamoDB scan on a large set of data. ?
Is there any other alternative? what are your thoughts and suggestions?
I'm trying to migrate those traffic data into Dynamodb table and then query it out. It seems like a bad idea to me now.
$params = [
'TableName' => $tableName,
'FilterExpression' => $this->filter.'=:'.$this->filter.' AND #dy > :since AND #dy < :now',
'ExpressionAttributeNames'=> [ '#dy' => 'day' ],
'ExpressionAttributeValues'=> $eav
];
var_dump($params);
try {
$result = $dynamodb->scan($params);
After considering the suggestion this is what worked for me
$params = [
'TableName' => $tableName,
'IndexName' => self::GLOBAL_SECONDARY_INDEX_NAME,
'ProjectionExpression' => '#dy, t_counter , traffic_type_id', 'KeyConditionExpression' => 'country=:country AND #dy between :since AND :to',
'FilterExpression' => 'traffic_type_id=:traffic_type_id' 'ExpressionAttributeNames' => ['#dy' => 'day'],
'ExpressionAttributeValues' => $eav
];
If your data is like Key-Value pair and you have fixed fields on which you want to index, use DynamoDB - you can create indexes on all fields you want to query and it will work great
If you require complex querying on multiple indexes, then any RDBMS is good.
If you can query on just about anything, think about Elastic search
If your queries are very simple, but you have large data to be retrieved in each query. Think about S3. Maybe you can index metadata in DynamoDb and actual data can be in S3
I'm performing a simple request to my MongoDB in php. It looks like this:
$result = $this->myMongoClient->myCollection->find([
'param_1' => $param_1,
'param_2' => $param_2,
]);
This returns a MongoDB\Driver\Cursor object. I need the count of returned entrys from the database.
I've googeld a while and found this. But with this function dose not exists on the object returend by MongoDB (Call to undefined method MongoDB\Driver\Cursor::count())
$result->count()
Which is the commun way to count the number of results?
I have found a solution, but don't know if it is recommendable or not.
count($result->toArray());
I don't know if you are using the MongoDB PHP Library, but if you are you can just use the following
$result = $this->myMongoClient->myCollection->countDocuments($where);
https://docs.mongodb.com/php-library/master/reference/method/MongoDBCollection-countDocuments/index.html
I am using MongoDB, with PHP YII. I have used YiiMongoDbSuite for setting up the criteria for mongoDB Queries.
Currently, I am using Group by and Limit together. But due to some reason queries are returning less number of rows than that are expected.
$criteria=new EMongoCriteria();
$criteria->group('col_1');
$criteria->limit(10);
$result = TableName::model()->findAll($criteria);
Can somebody guide me as I am quite new to MongoDB and YiiMongoDbSuite.
Thanks in advance,
Well to do it using MongoYii (which I maintain):
$result = MongoModel::model()->aggregate(
array(
'$group' => array('_id' => 'col_1'),
'$limit' => 10
)
)
I am unsure how to do it with YiiMongoDbSuite, in fact there is no group command in its EMongoCriteria from what I see.
i need to find the most recent element, the field "data_caricamento" save the date.
so i made a index with MognoDb shell:
db.collection.ensureIndex({"data_caricamento": -1})
and with the php code below i have what i need
$cursor=$collection->find();
$cursor->sort(array("data_caricamento"=> -1));
$cursor->limit($n);
but i think that should be a better way to do it,
for example there is a way to query the directly the index?
thx.
there is a way to query the directly the index?
Sort of. You can do a covered query here by doing:
$cursor = $collection
->find(array(), array('_id' => 0, 'data_caricamento' => 1))
->sort(array("data_caricamento" => -1))
->limit($n);
That will query only the index.
Try using the hint. And if you want to check whether the indexes got used by your query use explain().
....
$collection->find()->sort(array('data_caricamento'=>1))->hint(array('data_caricamento'=>1));
print_r($cursor->explain());
This will help you see that your queries are hitting index for faster search and sorting!
Cheers!
I have array like this
$conditions = array("Post.title" => "This is a post");
And using $conditions array in this method.
$this->Post->find('first', array('conditions' => $conditions));
I want convert the $conditions array to normal sql query.
I want use
$this->Post->query($converted_query);
instead of
$this->Post->find('first', array('conditions' => $conditions));
$null=null;
echo $this->getDataSource()->generateAssociationQuery($this, NULL, NULL, NULL, NULL, $query_array, false,$null);
To do what you want you could do two things:
1) Combine your $conditions arrays and let CakePHP build your new query so you can simply use $this->Model->find() again.
2) Use this. It's an expansion for the mysql datasource that adds the option to do $this->Model->find('sql', array('conditions' => $conditions)) which will return the SQL-query. This option might cause trouble, because for some find calls (especially when you're fetching associated models) CakePHP uses multiple queryies to fetch the associated models (especially in case of hasMany-associations).
If at all possible, option 1 will probably cause the least trouble. Another problem with going with 2 is that if you're trying to combine two queries with conflicting conditions (like 'name = Hansel' in query 1 and 'name = Gretel' in query 2) you will just find nothing unless you plan on writing extra code to parse the resulting queries and look for conflicts..
Going with 1 will probably be a lot simpler and will probably avoid lots of problems.