user48956
user48956

Reputation: 15788

Is it possible to refer to multiple documents in a mongo db query?

Suppose I have a collection containing the following documents:

...
{
   event_counter : 3 
   event_type: 50
   event_data: "yaya"
}
{
   event_counter : 4 
   event_type: 100
   event_data: "whowho"
}
...

Is it possible to ask for:

for each document, e where e.event_type == 100
    get me any document f where 
       f.event_counter = e.event_counter+1

or equivalently:

find each f, where f.event_counter==e.event_counter+1 && e.event_type==100

Upvotes: 0

Views: 68

Answers (1)

sfritter
sfritter

Reputation: 891

I think the best way for you to approach this is on the application side, using multiple queries. You would want to run a query to match all documents with e.event_type = 100, like this one:

db.collection.find({"e.event_type" : 100});

Then, you'll have to write some logic to iterate through the results and run more queries to find documents with the right value of f.event_counter.

I am not sure it's possible to do this using MongoDB's aggregation framework. If it is possible, it will be quite a complicated query.

Upvotes: 1

Related Questions