0


User will enter a input name and broadband Speed like below
Name: shankar
Speed:10Mbps
Then i need to display
Name:Shankar
Speed : 10Mbps
Data Used : 500MB

from below given sample mongodb document structure how to query in mongoDB?
Parent Record is :
{
subscriber_id : 001
name: shankar
total_data_used: 1000MB
Timestamp: 2016-Aug-01 00:03:00
}
{
subscriber_id : 002
name: John
total_data_used: 2000MB
Timestamp: 2016-Aug-01 00:10:00
}

Detail or child record as follows:
{
subscriber_id: 001
service:10Mbps
Timestamp: 2016-Aug-01 00:01:00
total_data_usage: 500MB
}


{
subscriber_id: 001
service:10Mbps
total_data_usage: 500MB
Timestamp: 2016-Aug-01 00:02:00
}


{
subscriber_id: 002
service:10Mbps
Timestamp: 2016-Aug-01 00:07:00
total_data_usage: 1000MB
}


{
subscriber_id: 002
service:10Mbps
total_data_usage: 1000MB
Timestamp: 2016-Aug-01 00:08:00
}

Anyone tell me in mongodb query how to retrieve

Regards,
Shankar

3
  • Not so sure about your question, if you wanna query the parent documents and children documents , and then join them in one document , mongodb don't support that. Maybe $aggregate can help but it's not how mongodb work. You have to make it in two query, one for parent, one for child Commented Aug 25, 2016 at 8:37
  • Looks like it may fit your requirement , but I am not sure about the performance . You'd better use an explain() to check the performance before you decide to use it. Commented Aug 25, 2016 at 10:22
  • ok ill try thanks john Commented Aug 25, 2016 at 11:03

1 Answer 1

1

This query should work. The first 'lookup' stage joins the child collection with the parent collection based on the subscriber_id field. It appends all documents in parent collection which match in the subscriber_details array of the child document.

In your case, this will be the output after the first stage.

{ subscriber_id: 001, service:10Mbps, Timestamp: 2016-Aug-01 00:01:00, total_data_usage: 500MB, subscriber_details:[{ subscriber_id : 001, name: shankar, total_data_used: 1000MB, Timestamp: 2016-Aug-01 00:03:00 }] }

{ subscriber_id: 001, service:10Mbps, total_data_usage: 500MB, Timestamp: 2016-Aug-01 00:02:00, subscriber_details:[{ subscriber_id : 001, name: shankar, total_data_used: 1000MB, Timestamp: 2016-Aug-01 00:03:00, }] }

After this stage, I am using a 'match' to filter out against your input conditions and then a 'project' stage to obtain the output in the form you desire.

db.child.aggregate([
{
   $lookup:
     {
       "from": "parent",
       "localField": "subscriber_id",
       "foreignField": "subscriber_id",
       "as": "subscriber_details"
     }
}, 
{
   $match:
     {
       "service":"<input_speed>",
       "subscriber_details.name":"<input_name>"
     }
},
{
   $project:
     {
       "_id":0
       "Name":"$subscriber_details.0.name"
       "Speed":"$service"
       "Data Used":"$total_data_usage"
     }
}]) 
Sign up to request clarification or add additional context in comments.

Comments

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.