How to implement "SELECT * FROM Users WHERE ID NOT IN(4,5,6)" in Lucene
try:
NOT(ID:(4 OR 5 OR 6)) AND *:*
The Lucene QueryParser will generate a query for that expression that gives similar results to the SQL query you gave. The AND : is necessary since Lucene won't give any results back for a purely negative query.
Here's how to do it using just the Lucene Query-based objects. You'd use MUST_NOT/- in conjunction with a MatchAllDocsQuery. (See the answer for this question for more info on why a MatchAllDocsQuery is needed.)
Your query would then look something like this:
*:* -ID:4 -ID:5 -ID:6
Related
I need to skip a number of documents (offset) from a query, and only return limit number of documents that go after. I know the following naive approach:
collection.find(BSONDocument())
.cursor[T].collect[List](offset+limit).map(_.drop(offset))
but it is not really desired because it will load offset+limit number of documents in JVM memory, whereas I'd like to filter them on the "database" side.
Solution: use QueryOpts. Example:
collection.find(BSONDocument())
.options(QueryOpts(skipN = offset))
.cursor[T].collect[List](limit)
Note that using skip is not very efficient because mongodb does not support effective pagination, it will just skip the desired number by iterating through all the documents.
VasyaNovikov answer is certainly correct. Reactive mongo offers a more intuitive API:
collection.find(BSONDocument())
.skip(offset)
.cursor[T]
.collect[List](limit, Cursor.FailOnError[List[T]]())
As we already known as we don't have table joins in Mongodb but if we want to get result from 2 different documents than how we can query to Mongodb? Consider following example.
Document 1 - > department
{Id_:123,name:technical,location:"B Wing"}
{Id_:234,name:account,location:"main Wing"}
{Id_:547,name:HR,location"C Wing"}
Document 2 - > employee
{Id_:a101,name:Peter,dept_id:234,DOB:2010-01-01}
{Id_:a102,name:Liomo,dept_id:547,DOB:1950-01-01}
{Id_:a103,name:Juno,dept_id:123,DOB:1990-01-01}
{Id_:a104,name:ole,dept_id:554,DOB:2011-01-01}
So how can we get all fields like (EmployeeName, DepatmentName, DOB) in one result, I am not getting any way please help me out
Thanks in advance
There is no way. Mongodb does not support joins. The NoSQL way is to denormalize your data, meaning you embed a copy in A of the fields from B you need in A.
There is such a thing as database references, but all that does is provide syntactic sugar for combining the result of two queries client side.
By the way, if your data is really relational in nature (like an employee database the way I would probably design it), perhaps a relational database would be a better fit.
I have the following problem: I can't limit number of results when using distinct. Exemple :
$stores = $this->dm->createQueryBuilder('Application\Document\Item')
->distinct('storeName')
->limit(10)
->getQuery()
->execute();
This query render 100 entries but I want only 10 results.
With query builder class in ORM you need to use:
->setMaxResults(10);
As per #Siol and #john Smith said, in ODM you could use limit:
->limit(10);
I don't think distinct will work with limit as suggested in the Jira mongodb issue ticket Ability to use Limit() with Distinct():
The current Distinct() implementation only allows for bringing back
ALL distinct values in the collection or matching a query, but there
is no way to limit these results. This would be very convenient and
there are many use cases.
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...
I have been looking if it is possible to do a GROUP BY filter on Zend Lucene search results similar to MySQL so that if it returns some rows that have the same specified field, it will only return it is 1 row.
I am not sure about Zend, but in Lucene/Solr world this feature is called "field collapsing".
A few pointers:
Example with code
JIRA issue
Grouping collector