Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB $lookup returns empty array [duplicate]

When i am trying to JOIN in mongo db the $lookup returns the empty array.

I have two collection one is user_information and another one is add_to_cart.In that i want to get add_to_cart details of user form add_to_cart collection using user_id in user_information.

add_to_cart collection:

[
  {
    "_id": {
      "$id": "592ec12b744a12d014000031"
    },
    "order_id": "592ec125744a12d014000030",
    "table_id": 1,
    "category_name": "veg",
    "food_id": "5923c8bc744a12441e000031",
    "user_id": "592ec125744a12d01400002f",
    "food_name": "Cream Of Mushroom Soup",
    "food_per_price": "100",
    "food_total_price": 100,
    "food_qty": 1,
    "active_status": 0,
    "created_at": {
      "sec": 1496236331,
      "usec": 0
    },
    "updated_at": {
      "sec": 1496236331,
      "usec": 0
    }
  },
  {
    "_id": {
      "$id": "592ec12e744a12d014000032"
    },
    "order_id": "592ec125744a12d014000030",
    "table_id": 1,
    "category_name": "veg",
    "food_id": "5923c8cb744a12441e000033",
    "user_id": "592ec125744a12d01400002f",
    "food_name": "Cream Of Mushroom Soup",
    "food_per_price": "100",
    "food_total_price": 100,
    "food_qty": 1,
    "active_status": 0,
    "created_at": {
      "sec": 1496236334,
      "usec": 0
    },
    "updated_at": {
      "sec": 1496236334,
      "usec": 0
    }
  }
]

user_information collection:

[
  {
    "_id": {
      "$id": "592ec125744a12d01400002f"
    },
    "branch_id": 1,
    "brand_id": 1,
    "business_id": 1,
    "table_id": 1,
    "uid": "116907438816775509716",
    "user_name": "dhamo dharan",
    "user_email": "[email protected]",
    "user_provider": "google",
    "user_image": "https://lh5.googleusercontent.com/-Masl6FTlG_g/AAAAAAAAAAI/AAAAAAAAAEo/UV3oTjMnqzQ/s96-c/photo.jpg",
    "active_status": 0,
    "created_at": {
      "sec": 1496236325,
      "usec": 0
    },
    "updated_at": {
      "sec": 1496236325,
      "usec": 0
    }
  }
]

my database query

db.add_to_cart.aggregate([
  { "$match": { "user_id": "592ec125744a12d01400002f" } },
  { "$sort": { "created_at": -1 } },
  { "$limit": 20 },
  { "$lookup": {
    "from": "user_information",
    "localField": "user_id",
    "foreignField": "_id",
    "as": "userinfo"
  } },
  { "$unwind": "$userinfo" },
  { "$project": {
    "food_name": 1,
    "food_qty": 1,
    "userinfo.user_name": 1,
    "userinfo.user_email": 1
  } }
])

It will return empty result i don't know what went wrong.Thanks in advance!

like image 502
dhamo dharan Avatar asked May 31 '17 13:05

dhamo dharan


1 Answers

In Simple Words - your match query is wrong .

Because "user_id": "592ec125744a12d01400002f" is a "string" , matching works fine with normal find query with string ObjectId

but when we talk about aggregate , you can not give direct string .. you always give mongoose.Types.ObjectId(userId) , where userId is string

var userId="592ec125744a12d01400002f";


db.add_to_cart.aggregate([
{ "$match": { "user_id": mongoose.Types.ObjectId(userId) } },
{ "$sort": { "created_at": -1 } },
{ "$limit": 20 },
{ "$lookup": {
"from": "user_information",
"localField": "user_id",
"foreignField": "_id",
"as": "userinfo"
} },
{ "$unwind": "$userinfo" },
{ "$project": {
"food_name": 1,
"food_qty": 1,
"userinfo.user_name": 1,
"userinfo.user_email": 1
} }
])
like image 189
Shashwat Gupta Avatar answered Nov 09 '22 23:11

Shashwat Gupta