distinct values from the yii mongodb collection - mongodb

How could I get the distinct values from the mongodb collection in yii.
Is it same as that in sql ie can I use distinct(fieldname) in yii.

In Yii I believe you would have to access the mongodb object directly and work on it using the distinct() command:
Yii::app()->mongodb->command(array("distinct" => "your_field", "key" => "your_key"))
This will not return an AR format of the results since MongoDBs distinct doesn't quite work like SQLs however you could emulate something of SQLs distinct with the aggregation framework with most likely $group on your field.
There might be a way of doing this with whatever plugin you are using in Yii however we have no idea other than the fact that you are using Yii...

Related

Cloudant : query in http navigator

I'm using cloudant, with no auth, Cors enabled.
it works very well, Limit and skip working good.
but i can't find how to search for something .
I'm trying to find a document where cp is 24000 , for example with this query :
https://1c54473b-be6e-42d6-b914-d0ecae937981-bluemix.cloudant.com/etablissements/_all_docs?skip=0&limit=10&include_docs=true&q=cp:24000
But, the query doesn't return the right document.
I've also tried
https://1c54473b-be6e-42d6-b914-d0ecae937981-bluemix.cloudant.com/etablissements/_all_docs?skip=0&limit=10&include_docs=true&_search({'cp':24000})
with no luck.
oh, and by the way, do you know if jquery.couch.js lib has been discontinued? I cant even find it on github, nor on my hard disk while im using foxant, and it is not in the directory also..
The /db/_all_docs endpoint hits the primary index of the database where all of the documents in the database can be found in _id order.
If you wish to query the database to get a subset of the data you have three options
Cloudant Query - hit the POST /db/_find endpoint passing in a JavaScript object containing the selector which defines the query you wish to perform (like the WHERE clause of a SQL query) e.g. {selector: {cp: 24000}}
MapReduce - create a Map function in a design document that filters the documents you are interested it. It creates a materialized view that can be queried and filtered later. e.g. function(doc){ emit(doc.cp, null);}
Cloudant Search - this uses the Apache Lucene library to generate an index on the fields you specify. You can then query the index: q=cp:24000, which looks similar to the query you are looking to perform.

How to retrieve whole document in mongodb distinct query rather than only the key?

Mongodb distinct command returns the list of distinct keys in a given document.
Is there any way to retrieve the whole document rather than only keys using mongodb java driver or morphia?
In the documentation it says:
When possible, the distinct command will use an index to find the documents in the query as well as to return the data.
How does this work, when I try using java driver it always returns the list of keys to my queries. And it doesn't take limit and order queries into consideration. Is it possible to give a order by and limit queries into distinct query?
Thanks for the feedback.

Sort by reference document in Doctrine MongoDB ODM

I'm using DoctrineMongoDBOBundle with Symfony2.
I've a Document Product which has an annotation referenceOne to other Document Price.
I want to sort by price when I fetch with queryBuilder.
$qb = $dm->createQueryBuilder('MyBundle:Product')
->field('geocoordinates')
->near('lat','lon')
->sort('hasPrice','desc')
But this doesn't works. Perhaps for the use of near?
It depends of toString() method of Document Price?
Regards.
I've a Document Product which has an annotation referenceOne to other Document Price.
There are no joins in MongoDB and I do not believe Doctrine does client side aggregation and sorting here. As such this wouldn't work anyway.
However sorting will work on a $near command ( http://docs.mongodb.org/manual/reference/operator/near/ ) which is what Doctrine should be using in this case, here you can see explicit support for $near being identified through the command you are using: https://github.com/doctrine/mongodb/commit/59f73cde2c15d171ff39afbf46c1a1339a51048c so your problem is the linked document, MongoDB has no JOINs.
In this case, hasPrice looks like it corresponds to a method, that perhaps checks whether the price reference is null or not. When referring to fields in ODM queries, names of class properties may be translated to the MongoDB field names (if they differ), but there is no support for method evaluation. Sammaye was correct that Doctrine does no client-side aggregation or sorting.
As an alternative, you may be able to sort on the price.$id field, which should group nonexistent values together in the results. Similarly, you could use the $exists operator to match/exclude based on whether a document was actual referenced. References in ODM (excluding the "Simple" type) are stored as MongoDBRef instances, which translate to objects with $id, $ref, and $db fields. As a result, you can query on those values just like any other field in your document.

How do I query for a specific MongoDB collection field inside Rails Console?

I have a Rails 3 app using MongoDB, with Mongoid as the ORM. I'd like to query for a specific field within a collection.
To query for all records of a particular collection I use User.all.to_a, as an equivalent to User.all in ActiveRecord.
Now I'd like to query for all records within a collection, but only output a particular field. In this case I'd like to see all User names. How do I do this?
I'm sure I've stared right at this in the Mongoid documentation and am just missing something...
I couldn't locate it in the new documentation for mongoid, but here is a quick link to only pointing to old 2.x.x documentation.
Basically you need to do:
User.all.only(:name).to_a

MongoDB: How to execute a query to result of another query (nested queries)?

I need to apply a set of filters (queries) to a collection. By default, the MongoDB applies AND operator to all queries submitted to find function. Instead of whole AND I need to apply each query sequentially (one by one). That is, I need to run the first-query and get a set of documents, run the second-query to result of first-query, and so on.
Is this Possible?
db.list.find({..q1..}).find({..q2..}).find({..q3..});
Instead Of:
db.list.find({..q1..}, {..q2..}, {..q3..});
Why do I need this?
Bcoz, the second-query needs to apply an aggregate function to result of first-query, instead of applying the aggregate to whole collection.
Yes this is possible in MongoDB. You can write nested queries as per the requirement.Even in my application I created nested MongoDb queries.If you are familiar with SQL syntax then compare this with in of sql syntax:
select cname from table where cid in (select .....)
In the same way you can create nested MongoDB queries on different collections also.