0

I am trying to setup a merge with MongoDb for 2 collections. Collections are nested. I have 2 collections:

1/ Options

{
    "_id": {
        "$oid": "58c9866ff36d286bfca335b4"
    },
    "design": {
        "mainPage": {
            "imgRight": "58cc750ddc238d05dd6ca525",
            "imgLeft": "58cc750ddc238d05dd6ca526"
        }
    }
}

2/ Forms

{
    "_id": {
        "$oid": "58cc750ddc238d05dd6ca525"
    },
    "imagePath": "logo.png"
}

How can I do an aggregate to get this result:

{
    "_id": {
        "$oid": "58c9866ff36d286bfca335b4"
    },
    "design": {
        "mainPage": {
            "imgRight": "58d07571d9d39e50166b5b3d",
            "form_docs" : {
                "imagePath": "logo.png"
            }
            "imgLeft": "58cc750ddc238d05dd6ca526",
            "form_docs" : {
                "imagePath": ""
            }
        }
    }
}

I am trying with the snippet below, but it doesn't work:

   Options.aggregate([
        { "$unwind": "$design" },
        { "$unwind": "$design.mainPage" },

        {
          $lookup:
            {
              from: "forms",
              localField: "design.mainPage.imgLeft",
              foreignField: "_id",
              as: "form_docs"
            }
       }

])
s7vr
  • 73,656
  • 11
  • 106
  • 127
Alan
  • 9,167
  • 4
  • 52
  • 70
  • You don't have to unwind nested document. The type of the field you are joining on should be same in both local and foreign collection. Currently its `ObjectId` and `String` – s7vr Mar 21 '17 at 18:04
  • Thanks. When I just do: `Options.aggregate([ { $lookup: { from: "forms", localField: "design.mainPage.imgLeft", foreignField: "_id", as: "form_docs" } } ])` it is not working also.. – Alan Mar 21 '17 at 18:07
  • 1
    It is not going to because the type for `design.mainPage.imgLeft` (String) is different from `_id` (ObjectId) – s7vr Mar 21 '17 at 18:09
  • I guess answer is here.. https://jira.mongodb.org/browse/SERVER-22781 – Alan Mar 22 '17 at 02:17
  • The alternative answer is to use data consistently across the db; if the value in the _forms_ collection is `ObjectId("58cc750ddc238d05dd6ca525")`, then the matching value in the _options_ collection should also be `ObjectId("58cc750ddc238d05dd6ca525")` (ObjectId data type), not `"58cc750ddc238d05dd6ca525"` (string data type). – Vince Bowdren Mar 22 '17 at 11:04
  • @Veeram You should post that as your answer. – Vince Bowdren Mar 22 '17 at 11:10
  • I have solve this issue by using mongoose: http://mongoosejs.com/docs/populate.html `router.get('/', function (req, res, next) { // doenset work http://stackoverflow.com/questions/36022456/mongodb-lookup-on-nested-document Options .findOne() .populate('design.mainPage._imgLeft') .populate('design.mainPage._imgRight') .exec(function (err, obj) { if (err) return handleError(err); return res.status(200).json({ message: 'Successfull', obj: obj }) }); });` – Alan Mar 22 '17 at 17:00

1 Answers1

0

I have solded my issue by using Mongoose with populate(). http://mongoosejs.com/docs/populate.html

1/ Define the schema:

var options = new Schema({
    design: {
      mainPage : {
        _imgLeft:[{type: Schema.Types.ObjectId, ref: 'Form'}],
        _imgRight:[{type: Schema.Types.ObjectId, ref: 'Form'}],
      }
    },
  }
);

2/ mongoose function is:

  Options
  .findOne()
  .populate('design.mainPage._imgLeft')
  .populate('design.mainPage._imgRight')
  .exec(function (err, obj) {
    return res.status(200).json({
      message: 'Successfull',
      obj: obj
    })
  });

Thanks for your help

Alan
  • 9,167
  • 4
  • 52
  • 70