I can get my elements, with the following code :
$lines = $loc->find(
array("loc" =>
array('$near' =>
array('$geometry' =>
array('type' => 'Point', 'coordinates' =>
array(floatval($longitude), floatval($latitude))
),
'$maxDistance' => 10000 //meters
)
)
)
);
This is the results :
Array
(
[_id] => MongoId Object
(
[$id] => 5490003c815289663d8bbd95
)
[name] => My adress
[loc] => Array
(
[type] => Point
[coordinates] => Array
(
[0] => 5.050948
[1] => 45.040419
)
)
)
But now, I need to get the distance from my given point.
Is it possible ?
EDIT :
This is the code used to insert datas :
$loc = $client->localisation->localisation;
$loc->ensureIndex( array("loc" => "2dsphere"));
$loc->insert(
array(
"idobject" => $myValue
"name" => $myName
"loc" => array("type" => "Point", "coordinates" => array($longitude, $latitude))
));
The $near operator does not return a distance along with the returned results. It only orders the documents in the response.
Under the PHP driver the easiest form is to use the $geoNear aggregation pipeline stage instead. This allows you to project a field in the results, or even use it in other pipeline stages:
$loc->aggregate(
array(
array(
'$geoNear' => array(
'near' => array(
'type' => 'Point',
'coordinates' =>array(floatval($longitude), floatval($latitude))
),
'maxDistance' => 1000,
'distanceField' => 'distance',
'spherical' => true
)
)
)
);
There is is a database command form for geoNear as well, but direct command results are often not as nice.
Related
im using yii1 on my application.
i want to convert from cActivedataProvider to array
this is the code
$dataSS = new CActiveDataProvider('category', array(
'criteria' => array(
'condition' => 'menu=:menu',
'params' => array(':menu' => $menu),
),
'pagination' => false
));
$dataMenu = array();
foreach ($dataSS->getData() as $record) {
$dataMenu[] = array(
'label' => $record->name,
'url' => '#',
);
}
this is the result :
Array (
[0] => Array ( [label] => Food and Drink [url] => # )
[1] => Array ( [label] => Sleman [url] => # )
)
the result that i expected :
Array (
Array ( 'label' => 'Food and Drink', 'url' => '#' ) ,
Array ( 'label' => 'Sleman', 'url' => '#' ) ,
)
any suggestion?
Finally get the answer,
this is really my bad because i call the function on wrong way.
This is the wrong way :
'items' =>array(Category::model()->getMenu("2");),
and this is the correct way :
'items' =>Category::model()->getMenu("2"),
I have the db with the following format
Array
(
[_id] => MongoId Object
(
[$id] => 53f4bf0e8db0d31b0ba802df
)
[userSession] => 580929792589634763f964479eee8721
[pageEnteredDate] => 1408548587
[pageLeftDate] => 1408548622
[userName] => User 1
[userId] => 33657
[pageView] => monitoring patients
[pageActions] => []
[pageTag] => 1-3-16-131-315
[timeSpent] => 35
)
Array
(
[_id] => MongoId Object
(
[$id] => 53f3d7008db0d33e61cae841
)
[userSession] => e04e5081c9482654030bacf3c8c90b21
[pageEnteredDate] => 1408488536
[pageLeftDate] => 1408489216
[userName] => user 2
[userId] => 4278
[pageView] => Surgery Staff
[pageActions] => [["BUTTON","Comment",1408488701],["A","Discussion",1408488712]]
[pageTag] => 1-3-5-148
[timeSpent] => 680
)
Array
(
[_id] => MongoId Object
(
[$id] => 53f3d7008db0d33gj1cae841
)
[userSession] => e04e5081c9482654030bahjhc8c90b21
[pageEnteredDate] => 1408488536
[pageLeftDate] => 1408489216
[userName] => user 3
[userId] => 428
[pageView] => Surgery Staff
[pageActions] => [["BUTTON","Comment",1408488701],["A","Discussion",1408488712]]
[pageTag] => 1-3-5-148
[timeSpent] => 680
)
pageEnteredDate is the date that i want to use
I want to group the data by pagetag and day. I mean on one day i should get the same pageTag only once.
So, from those 3 arrays it should display only 2 because 2 have the same pageTag and are on the same day.
Thanks
***code used
$result = $this->collection->aggregate(
array(
array(
'$group' => array(
'_id'=> array( 'pageTag' => '$pageTag','day' => array('$subtract' => array('$pageEnteredDate', 86400))),
'timeSpent' => array( '$sum' => '$timeSpent' ),
'lastView' => array( '$max' => '$pageEnteredDate' )
)
),
array('$skip' => 0),
array('$limit' => 20)
)
);
Your "date" values appear just to be numbers derived from epoch timestamps ( without the milliseconds ). But all you really want to do here is apply $group via the aggregation framework, and a little date math to group by "day":
$result = $collection->aggregate(array(
array(
'$group' => array(
'_id' => array(
'pageTag' => 'pageTag',
'day' => array(
'$subtract' => array(
'$pageEnteredDate',
array('$mod' => array(
'$pageEnteredDate',
60 * 60 * 24
))
)
)
),
'timeSpent' => array( '$sum' => '$timeSpent' ),
'lastView' => array( '$max' => '$pageEnteredDate' )
)
)
));
That basically says to group on both the "pageTag" values and using the "pageEnteredDate" to apply the math that essentially rounds the timestamp to the current day, so all values within the same day are the same.
You haven't said exactly what it is that you want to "group" here, so the examples are given of applying a $sum to the "timeSpent" value and using $max to identify the last timestamp value recorded on that day.
You can use any of the "grouping operators" in this way to suit your needs
I am pretty new to Elasticsearch, so apologies if this is is an obvious question!
I am using the PHP library and trying to do a pretty straightforward query across an index. The only thing I wanted to do was perform a filter against two dates (visible_from and visible_to) which I have set to date types when creating the mapping.
I am building up a filtered query in the code, which ends up looking like this:
array (
'index' => 'site',
'from' => 0,
'size' => 10,
'body' =>
array (
'query' =>
array (
'filtered' =>
array (
'filter' =>
array (
'and' =>
array (
'range' =>
array (
'visible_from' =>
array (
'lt' => '2014-06-09 09:06:47',
),
'visible_to' =>
array (
'gt' => '2014-06-09 09:06:47',
),
),
),
),
'query' =>
array (
'match' =>
array (
'_all' => 'example',
),
),
),
),
),
)
This results in a BadRequest400Exception being thrown - in the content of the exception I can see that Elasticssearch is saying
QueryParsingException[[site] [and] filter does not support [lt]]
If I remove the AND filter and just perform the range filter against one of the dates, then it works perfectly, I am just unable to get it working with the AND filter
Thanks in advance for any help!
Make a try with below query, Try with different less than and greater than date.
array
(
"query" => array
(
"filtered" => array
(
"query" => array
(
"match" => array(
'_all' => 'example',
),
),
"filter" => array
(
"and" => array
(
"filters" => array
(
"0" => array
(
"range" => array
(
"ID" => array
(
"gt" => '2014-06-09 09:06:47',
),
),
),
"1" => array
(
"range" => array
(
"ID" => array
(
"lt" => '2014-06-09 09:06:47',
),
),
),
),
),
),
),
),
)
Here's what the document looks like now.
Array
(
[email] => admin#mysite.com
[emailQueries] => Array
(
[0] => Array
(
[21] => 0
)
[1] => Array
(
[21] => 0
)
)
[last_visit] => 1375050871
)
Here's the code I am using to populate the emailQueries
$arrayValueToAdd = array( (string) $email_id => '0' );
$collection->update( array('email' => $user['email']),
array( '$push' =>
array( 'emailQueries' => $arrayValueToAdd )
)
);
However, I'd like to simply have the emailQueries array look like:
[emailQueries] => Array
(
[21] => 0
[22] => 0
)
Probably a simple thing I'm overlooking...
You are using the array operator push with a data structure that is not considered by MongoDb as an array (array must have 0-based ascending numeric indexes). You must treat it as an object.
So you have 2 choices.
If in [21] => 0, the value 0 is useless (and I don't expect so), you can use a true array:
"emailQueries" : [21,22]
and you can use the push operator in the PHP code like this
$collection->update( array('email' => $user['email']),
array( '$push' =>
array( 'emailQueries' => (string) $email_id )
));
On the other hand, if you must keep the value 0, and for instance, increment it, you have to considered your EmailQuery as an object :
"emailQueries" : {
"21" : 0,
"22" : 0
}
So you can use a simple update to add field in emailQueries :
$collection->update( array('email' => $user['email']),
array( '$set' =>
array( 'emailQueries.'.$email_id => 0 )
));
And the $inc, for incrementation :
$collection->update( array('email' => $user['email']),
array( '$inc' =>
array( 'emailQueries.'.$email_id => 1 )
));
In MongoDB-PHP I am using the following example code to push a new entry to the end of an array inside a collection...
$data = array(
"domain"=>"superduperyoyo.com",
"number"=>123,
"week"=>5,
"year"=>2012
);
$db->domains->save(
array( 'someid' => $someid),
array( '$push' => array( 'data' => $data ))
);
This returns keys like 0,1,2,3....
ie.
[someid] => somesupercoolid123
[data] => Array
(
[0] => Array
(
[domain] => superduperyoyo.com
[number] => 123
[week] => 5
[year] => 2012
)
[1] => Array(...)
[2] => Array(...)
)
What I want to do is store YearWeekNumber as the key like this...
[someid] => somesupercoolid123
[data] => Array
(
[201205123] => Array
(
[domain] => superduperyoyo.com
[number] => 123
[week] => 5
[year] => 2012
)
[201206123] => Array(...)
[201207123] => Array(...)
)
How do you save/update the key along with the new entry? I am assuming you can't use $push. That you just use .save or .update but how do you pass the key?
You'd do this by using $set:
$data = array(
"domain"=>"superduperyoyo.com",
"number"=>123,
"week"=>5,
"year"=>2012
);
$db->domains->update(
array( 'someid' => $someid),
array( '$set' => array( 'data' => array( 201205123 => $data )))
);
I would however not recommend doing this. It's better to set another key with this "201205123" value as otherwise you wouldn't be able to do range queries on this value or set an index.
$data = array(
"domain"=>"superduperyoyo.com",
"number"=>123,
"week"=>5,
"year"=>2012
);
$update = array(
'$push' => array('data.201205123' => $data )
);