Combine two collection to one use $loopup in vsc

db.getCollection(collection).aggregate([{$lookup: {
    from: 'Price',
    localField: 'id',
    foreignField: 'id',
    as: 'result'
  }},{$unwind: '$result'
  },{$addFields: {
      price: '$result.onsiterate',
      priceDiscount:'$result.discount',
      discount:'$result.discount',
      rateType:'$result.ratetype',
      isRatePerStay:'$result.israteperstay',
      mealType:'$result.mealinclusiontype',
      taxType:'$result.taxtype',
  }},{$project: {
      id:1,
      hotelcode:1,
      roomamenities:1,
      roomtype:1,
      ratedescription:1,
      price:1,
      priceDiscount:1,
      discount:1,
      rateType:1,
      isRatePerStay:1,
      mealType:1,
      taxType:1,
  }},{$out: 'Combine'}]);

error 'operation exceeded time limit ’ helpme

Hi @Anh_Tu_n_Hu_nh_Van and welcome to the MongoDB community forum!!

Based on your shared aggregation pipeline, I think you can optimise it by removing the $addFields stage since it duplicates the existing $project stage and may not be necessary.

db.getCollection(collection).aggregate([
        {
            $lookup: {
                from: 'Price',
                localField: 'id',
                foreignField: 'id',
                as: 'result'
            }
        },
        {
            $unwind: '$result'
        },
        {
            $project: {
                id: 1,
                hotelcode: 1,
                roomamenities: 1,
                roomtype: 1,
                ratedescription: 1,
                price: '$result.onsiterate',
                priceDiscount: '$result.discount',
                discount: '$result.discount',
                rateType: '$result.ratetype',
                isRatePerStay: '$result.israteperstay',
                mealType: '$result.mealinclusiontype',
                taxType: '$result.taxtype'
            }
        },
        {
            $out: 'Combine'
        }
    ]);

It simplifies the $project stage by directly mapping the fields to the output.

This error occurs when the if the time limit is reached before the operation completes.
For more details you can visit the documentation for Adjust Maximum Time for Query Operations.

However, if the issue still persist please share the following information to help you with appropriate query.

  1. A sample document in the collection.
  2. The desired output for the query.
  3. The MongoDB version you are on.

Regards
Aasawari

2 Likes

Thank you very much ,i use this to combine 3 three collections

db.getCollection('users').aggregate([
  { $match: { name: 'Giles.Welch' } },
  {
    $lookup: {
      from: 'Hotels',
      localField: '_id',
      foreignField: 'userId',
      as: 'hotels',
    },
  },
  { $unwind: '$hotels' },
  {
    $lookup: {
      from: 'RoomTypes',
      localField: 'hotels.roomTypeIds',
      foreignField: '_id',
      as: 'roomTypes',
    },
  },
  { $addFields: { 'hotels.roomTypeIds': '$roomTypes' } },
  { $project: { roomTypes: 0, password: 0, balance: 0 } },
]);

Any other way ??

Hi @Anh_Tu_n_Hu_nh_Van

Can you help me with a sample data for all the collections and the desired output which would help me to reproduce in my local environment.

Also, mention the MongoDB version you are on.

Ragards
Aasawari

1 Like

my version version v3.6.8
My data :

  {
    "_id": {
      "$oid": "642e9659793554fd9f944ec0"
    },
    "name": "Vernice90",
    "email": "Cesar21@yahoo.com",
    "verify": true,
    "avatar": "https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/997.jpg",
    "position": "HOTELIER",
    "isActive": true,
    "createAt": {
      "$date": "2023-04-06T09:52:17.612Z"
    },
    "updateAt": {
      "$date": "2023-04-06T09:52:17.612Z"
    },
    "hotels": {
      "_id": {
        "$oid": "642c63166c0f6bdd3b02927d"
      },
      "address": "121 Southgate Street",
      "city": "Gloucester",
      "country": "United Kingdom",
      "latitude": 51.86043167,
      "longitude": -2.250770092,
      "roomTypeIds": [
        {
          "_id": {
            "$oid": "642c63766c0f6bdd3b046092"
          },
          "price": 49.42,
          "priceDiscount": 0,
          "discount": 0,
          "mealType": "Free Breakfast",
          "images": [],
          "rateDescription": "Room size: 25 m²/269 ft², Shared bathroom, 1 single bed",
          "roomAmenities": [
            "Air conditioning",
            "Carpeting",
            "Clothes rack",
            "Fan",
            "Free Wi-Fi in all rooms!",
            "Hair dryer",
            "Heating",
            "In-room safe box",
            "Linens",
            "Shower",
            "Smoke detector",
            "Toiletries",
            "Towels",
            "TV [flat screen]"
          ],
          "updatedAt": {
            "$date": "2023-04-05T18:03:48.767Z"
          },
          "numberOfRoom": 5,
          "nameOfRoom": "Single Room with Shared Bathroom"
        },
        {
          "_id": {
            "$oid": "642c63766c0f6bdd3b046093"
          },
          "price": 57.02,
          "priceDiscount": 0,
          "discount": 0,
          "mealType": "Free Breakfast",
          "images": [],
          "rateDescription": "Room size: 36 m²/388 ft², Shared bathroom, 2 single beds",
          "roomAmenities": [
            "Air conditioning",
            "Carpeting",
            "Clothes rack",
            "Fan",
            "Free Wi-Fi in all rooms!",
            "Hair dryer",
            "Heating",
            "In-room safe box",
            "Linens",
            "Shower",
            "Smoke detector",
            "Toiletries",
            "Towels",
            "TV [flat screen]"
          ],
          "updatedAt": {
            "$date": "2023-04-05T18:03:48.767Z"
          },
          "numberOfRoom": 1,
          "nameOfRoom": "Twin Room with Shared Bathroom"
        },
        {
          "_id": {
            "$oid": "642c63766c0f6bdd3b046094"
          },
          "price": 82.36,
          "priceDiscount": 0,
          "discount": 0,
          "mealType": "Free breakfast for {2}",
          "images": [],
          "rateDescription": "Room size: 36 m²/388 ft², Shared bathroom, 2 single beds",
          "roomAmenities": [
            "Air conditioning",
            "Carpeting",
            "Clothes rack",
            "Fan",
            "Free Wi-Fi in all rooms!",
            "Hair dryer",
            "Heating",
            "In-room safe box",
            "Linens",
            "Shower",
            "Smoke detector",
            "Toiletries",
            "Towels",
            "TV [flat screen]"
          ],
          "updatedAt": {
            "$date": "2023-04-05T18:03:48.767Z"
          },
          "numberOfRoom": 8,
          "nameOfRoom": "Twin Room with Shared Bathroom"
        }
      ],
      "hotelName": "Spalite Hotel",
      "images": [],
      "starRating": 5,
      "start": 3,
      "propertyType": "Hotels",
      "userId": {
        "$oid": "642e9659793554fd9f944ec0"
      },
      "currency": "GBP",
      "createAt": {
        "$date": "2023-04-06T08:43:49.703Z"
      },
      "package": "YEAR"
    }
  }
]

Hello @Anh_Tu_n_Hu_nh_Van

Thank you for sharing the sample document.

I believe you have shared a user’s sample document that contains an embedded hotel and RoomTypes document. Could you please share three distinct sample documents from your respective collections, which, as I understand from the above-shared document, are users, Hotels , and RoomTypes ?

Based on the sample data shared,

  • Could you help me understand what are you trying to achieve here?
  • Or, Are you facing the same issue as the one mentioned in the initial post?

It would be helpful for us to assist you in a better way if you could help me with the specific details.

Furthermore, the mentioned version has reached the end of life (refer Legacy Support Policy), and would recommend you upgrade to the latest version for new features and bug fixes.

Regards
Aasawari

2 Likes