2

I have sub-events belonging to events belonging to an user, and I know the user's username - how do I get a list of the sub-events?

Is this going in the right direction? Or is it completely off?

db.subevents.find({_id: {$in: 
    db.events.find({_id: {$in:
        db.users.find({"username":"userx"},{_id:1})}},{_id:1})}})

Edit: Here is a sample of the data structure:

/* Event */
{
  "_id" : "XjhAqqNBkezKY3mdN",
  "name" : "My event",
  "userId" : "FiKsAAAgBb7cNoPH7"
}
/* Subevent */
{
  "_id" : "WkYAqBXNpJryp7rum",
  "name" : "The subevent",
  "eventId" : "hQXNzX3jbWppbAYFH"
}
/* User */
{
  "_id" : "RTHh5srhLMQp625zF",
  "username" : "userx"
}
11
  • 4
    could you post a sample of your data structure? Commented Jun 16, 2016 at 9:57
  • What is your MongoDB version? Commented Jun 16, 2016 at 10:47
  • It's MongoDB 3.0.7 Commented Jun 16, 2016 at 10:54
  • you cannon use such syntax in mongo - but thh will be a nice feature Commented Jun 16, 2016 at 10:57
  • 1
    @KaSh there is lookup over lookup - so that is not implemented even in 3.2 :-) Commented Jun 16, 2016 at 11:21

2 Answers 2

1

Following profesor79's advice to use three different calls, I put together this solution to get all the sub-events belonging to the user:

var userIds = db.users.find({"username":"userx"}).map(function(user) { 
  return user._id; 
});
var eventIds= db.events.find({userId: {$in:userIds}}).map(function(event) { 
  return event._id; 
});
db.subevents.find({eventId:{$in:eventIds}});
Sign up to request clarification or add additional context in comments.

Comments

0

The only way is to make 3 ddifrent calls in mongo 3.0.7

  1. GetUSerData
  2. GetEventData
  3. GetSubEventData

As you have relational schema here - this should be easy.

You could improve schema and remove subEvent collection by embedding subEvents documents into one events document

EDIT

/* Event */
{
    "_id" : "XjhAqqNBkezKY3mdN",
    "name" : "My event",
    "userId" : "FiKsAAAgBb7cNoPH7"
    "subewents" : [{
            "_id" : "WkYAqBXNpJryp7rum",
            "name" : "The subevent",
            "eventId" : "hQXNzX3jbWppbAYFH"
        }, {
            "_id" : "WkYAqBXNpJryp7rum2",
            "name" : "The subevent2",
            "eventId" : "hQXNzX3jbWppbAYFH"
        }, {
            "_id" : "WkYAqBXNpJryp7rum",
            "name" : "The subevent",
            "eventId" : "hQXNzX3jbWppbAYFH"
        }
    ]
}

/* User */
{
    "_id" : "RTHh5srhLMQp625zF",
    "username" : "userx"
}

3 Comments

So for example I should extract the eventIds into an array and then use that array to get the subEvent data? If it's not too much trouble, could you show us a little code sample?
@Cos - please see my edit with proposed structure - and could you tell me if this have a sense in your case
Thank you, however changing the schema is not an option for me at this point, I'd still be interested in the construction of the 3 different calls.

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.