Wrong distance in geonear method with Doctrine MongoDB ODM - mongodb

I'm using DoctrineMongoDBBundle with Symfony2 and I've a problem with geocoordinates. This works fine but when the longitude is for example like that: 0.635467 the code doesn't work. I have more geocoordinates and only fails when it begins with 0. and the distance field is NULL.
This is my code:
$locations = $dm->createQueryBuilder('MyBundle:Location')
->field('id')->in($arrayIds)
->field('geocoordinates')
->geoNear($geocodes['lat'],$geocodes['lon'])
->getQuery()->execute()->toArray();
I'm following this link: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/geospatial-queries.html but with the geonear method.

The geoNear() query builder method is not intended to be used on a field. near() is the builder method that would follow a field() focus. You can see what both of these builder methods do in Builder.php within the doctrine/mongodb project. Note that geoNear() changes the query type (similar to what update() does). The query type is then checked in Query.php (follow the switch statement) and determines how we issue the query on the collection. Some are actual query operations, but things like map/reduce and geoNear are commands.
See if the following code works:
$dm->createQueryBuilder('MyBundle:Location')
->geoNear($geocodes['lat'],$geocodes['lon'])
->field('id')->in($arrayIds)
->getQuery()->execute()->toArray();
If not, please debug the values that Query.php passes to the Collection::near() method. Alternatively, you can debug the entire query array generated by the builder by using the Query::getQuery() method.

Related

Retrieve a parent node using AEM query builder

I need to retrieve a parent node using AEM Query builder.
For instance, this is my query:
path:/content/test/us/bar
1_property:product
1_property.operation:exists
2_property:product
2_property.value:8003170008212
This query allow me to retrieve the following elements:
1) /content/test/us/bar/table/jcr:content/releated/product/2
2) /content/test/us/bar/chair/jcr:content/releated/product/1
Using this query, one can retrieve all elements placed under /content/test/us/bar which contain 8003170008212 as value of product property.
Starting from the previous bullet points I need to return just the parent, so for example:
1) /content/test/us/bar/table
2) /content/test/us/bar/chair
I can achieve my target programmatically, iterating the results and using 3 times the getParent() method.
I'am wondering: is there a way to get it with query builder?
If the property that you are searching for is always present at a known path, the query can be rewritten as
path=/content/test/us/bar
1_property=jcr:content/related/product
1_property.operation=exists
2_property=jcr:content/related/product
2_property.value=8003170008212
This would then result in
/content/test/us/bar/table
/content/test/us/bar/chair
which avoids looping through the result and finding the parent nodes.
For e.g., the following query in my local environment
path=/content/we-retail/language-masters/en
1_property=displayMode
1_property.operation=exists
2_property=displayMode
2_property.value=singleText
results in
/content/we-retail/language-masters/en/experience/wester-australia-by-camper-van/jcr:content/root/responsivegrid/contentfragment
/content/we-retail/language-masters/en/experience/arctic-surfing-in-lofoten/jcr:content/root/responsivegrid/contentfragment
/content/we-retail/language-masters/en/experience/steelhead-and-spines-in-alaska/jcr:content/root/responsivegrid/contentfragment
/content/we-retail/language-masters/en/experience/hours-of-wilderness/jcr:content/root/responsivegrid/contentfragment
/content/we-retail/language-masters/en/experience/skitouring/jcr:content/root/responsivegrid/contentfragment
/content/we-retail/language-masters/en/experience/fly-fishing-the-amazon/jcr:content/root/responsivegrid/contentfragment
But rewriting the following query to
path=/content/we-retail/language-masters/en
1_property=jcr:content/root/responsivegrid/contentfragment/displayMode
1_property.operation=exists
2_property=jcr:content/root/responsivegrid/contentfragment/displayMode
2_property.value=singleText
results in
/content/we-retail/language-masters/en/experience/wester-australia-by-camper-van
/content/we-retail/language-masters/en/experience/arctic-surfing-in-lofoten
/content/we-retail/language-masters/en/experience/steelhead-and-spines-in-alaska
/content/we-retail/language-masters/en/experience/hours-of-wilderness
/content/we-retail/language-masters/en/experience/skitouring
/content/we-retail/language-masters/en/experience/fly-fishing-the-amazon

Meteor/MongoDB limiting the result

I am trying to find all documents and publish at most 5 from the results.
Following this section of the MongoDB doc, I am trying to do this:
Meteor.publish('teams', function () {
return Teams.find().limit(5);
});
Yet, in the server console, I get an exception:
Exception from sub teams id Pm6jKL8Sv3FSDSTfM TypeError: Object [object Object] has no method 'limit'
The following works fine:
Meteor.publish('teams', function () {
return Teams.find({}, {limit:5});
});
Why does the second way work, rather than the first? And where can I find documentation for it?
Meteor's collection API is somewhat different from that of the mongo API. find takes up to two parameters: a selector object, and an options object. options allows you to specify such things as sort, skip, limit and fields, in addition to the meteor-specific reactive and transform.

FindBy property in TYPO3 Extbase MVC is not working

I'm unable to run the FindBy magic function property in Extbase MVC
$title=array(0 =>'Books Day');
$each_event=$this->eventRepository->findByTitle($title);
$each_event is returning an object of type TYPO3\CMS\Extbase\Persistence\Generic\QueryResult .
How do I make this work?
I also tried passing in a string to findByTitle and findByOne. Both don;t work! :(
I'm using TYPO3 6.1 and extension builder.
The last part of those magic functions always needs to be a field in the database. So "title" must be in your model. You might have a field "one" for your object, but I guess you meant findOneByTitle?
The object type QueryResult is correct. You can turn it into an array for debugging purpose for example:
$foo = $query->execute()->toArray();
By the way: check wether your eventRepository is null or not and you could try this to see if it works at all:
$result = $this->myRepository->findAll();
Try
$each_event=$this->eventRepository->findByTitle($title)->toArray();
Reference to the QueryResult.
As said in the documentation, it returns a QueryResultInterface|array.
As a consequence you have to loop over the result like this:
foreach($each_event as $single_event) {
$single_event->getProperty();
}
If you are sure that it returns only one single value you could also access it by the index 0:
$each_event[0]->getProperty();

Is it possible to iterate a mongo cursor twice?

In other words, is there a way to rewind it to the beginning?
EDIT
I am using mongo shell and pymongo.
The Ruby API has the rewind! method that does exactly what you want.
The Python API also has the cursor.rewind() method.
The PHP API also has the cursor.rewind() method.
However, neither the Java or C++ APIs have the rewind method. All can be found from the official API page.
Cursor in pymongo has .rewind() method, you can refer to sample code from previous question with answer that apply.
Native mongo shell api, however, doesn't provide such method, see method help() on DBQuery object prototype.:
> db.collection.find().help()
find() modifiers
.sort( {...} )
.limit( n )
.skip( n )
.count() - total # of objects matching query, ignores skip,limit
.size() - total # of objects cursor would return, honors skip,limit
.explain([verbose])
.hint(...)
.showDiskLoc() - adds a $diskLoc field to each returned object
Cursor methods
.forEach( func )
.map( func )
.hasNext()
.next()
You can use cursor.reset();
For PHP : $cursor->reset();
then run your foreach($cursorData as $data) any time after resetting.

PHP MongoDB, update document without erasing the rest

I'm trying to update a mongo document, using PHP and the update() function. However, when I do this, it replaces the WHOLE document with the value I wanted to update. How can I fix this?
The code I have written up to now: http://twaddlr.com/view/73
(Scroll down to the "update" function. It's a database wrapper I'm writting for my site)
The key is to use $set in the update, e.g. instead of this (sorry using JavaScript syntax here, not sure about the exact PHP driver syntax):
db.my_collection.update({hello: "world"}, {foo: "bar"})
you do
db.my_collection.update({hello: "world"}, {$set: {foo: "bar"}})
If you use $set, only the properties you specify will be updated, the whole document will not be replaced.
You can read more about this in the documentation, here: http://www.mongodb.org/display/DOCS/Updating#Updating-ModifierOperations
Edit: looking at your code, this is exactly what you do in the addRow method. Just do the same thing in update.