3

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.

2
  • First and second queries do the same operation. Seems it obvious.. Commented Dec 22, 2010 at 6:09
  • But first query is currently not possible in MongoDB. Thats what my question is how to achieve the first query. Commented Dec 24, 2010 at 6:19

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion. This solves the problem but have to be careful about performance of $in operator in nested queries.
Yes, definitely you need to be concern that factor also before writing nested queries.And if it solves your problem then accept it so others having same problem will get solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.