0

I have two collection: 1.app which have documents like below example

{
    "_id" : "1",
    "app_name" : "Cab",
    "active" : "A",
},
{
    "_id" : "2",
    "app_name" : "Gym",
    "active" : "A",
}

2.category which has documents like below example

{
    "_id" : "1",
    "app_id" : 1,
    "header" : "Request",
    "onbehalf" : "N",
    "project_onbehalf" : "N",

}

I want to perform something like

select app.app_id,app.app_name,category.header,category.onbehalf from app category where category.app_id=app._id AND app._id = "2"

EDIT

I have tried this query

db.category.aggregate([
                                        {"$lookup": {
                          "from": "app",
                          "localField": "_id",
                          "foreignField": "_id",
                          "as": "R"
                        }},
                        {"$unwind": "$R"},
                        {"$project": {
                          "midEq": {"$eq": ["$M_id", "$R.app_id"]},
                          "app_id": 1, "app_name": 1
                          }},
                        {"$match": {
                          "$and": [
                            {"category_id": {"$eq": 6}}, 
                            {"midEq": {"$eq": True}}
                        ]}},
                        {"$project": {
                          "midEq": 0
                        }}

                        ])

But I am getting script executed successfully,but there are no result to show, After executing the query.

Any Help will be appreciable.

Thanks In Advance.

  • Mongodb does not have joins, so you have to execute two different queries and "join" the results in memory. – ema Feb 28 '18 at 11:47
  • 1
    Fix to use same types of both app_id and _id either numeric or string and use $lookup from linked answer. – s7vr Feb 28 '18 at 12:00
  • @ema MongoDB has $lookup to perform joins. – Krishna Feb 28 '18 at 15:54
  • @Veeram As you suggested I have made both the id numeric and tried the query.But it is not giving me result.Can you suggest me what is wrong with the query. – Shweta Dwivedi Mar 05 '18 at 07:16
  • category_id field is not retained when you use project. Use `{"$addFields": { "midEq": {"$eq": ["$M_id", "$R.app_id"]}, "app_id": 1, "app_name": 1 }}` instead of $project. – s7vr Mar 05 '18 at 10:35

0 Answers0