How to do embedded documents using spring java mongo repository

I have one document from sample_collection1 as

{
	"_id": {
		"$oid": "5e597a2be08a070bab329ef3"
	},
	"field1": "value1",
	"field2": [{
		"field3": "1234123",
		"field4": [{
			"field5": "3432",
			"field6": ["5e5ac0008def101ffa037100"
			]
		}]
	}]
}

and other document from sample_collection2 in one of the following type as

{"_id":{"$oid":"5e5ac0008def101ffa037100”},”childfield1”:”5e5ac0008def101ffa037906","childfield2”:”childvalue2”,"childfield3”:”childvalue3”}

or as

{"_id":{"$oid":"5e5ac0008def101ffa037906”},”childfield1”:” 5e5ac0008def101ffa037100","childfield2”:”childvalue2”,"childfield3”:”childvalue3”}

how to get following response using spring mongo repository

{
	"_id": {
		"$oid": "5e597a2be08a070bab329ef3"
	},
	"field1": "value1",
	"field2": [{
		"field3": "1234123",
		"field4": [{
			"field5": "3432",
			"field6": [{”childfield1”:” ","childfield2”:”childvalue2”,"childfield3”:”childvalue3”}
			]
		}]
	}]
}

This is how you paste code/documents on here:

Or use <pre>code</pre> HTML tags

1 Like

There’s an aggregation pipeline operator called $lookup. Suggest you find the implementation in Spring Mongo.

PS: This channel is for learning Data Modelling. These questions about Spring Java Mongo Repository are probably best suited in a forum dedicated to Spring Java Mongo Repository.

Thanks for letting me know. Please provide complete lookup and replace compass aggregation stages or java code.

Please refer to the documentation for $lookup (link in my last post) for examples.

Please don’t suggest. I went through it but can’t find solution for my problem

@satishreddy,

Firstly, I must reiterate that these discussion boards (or if you prefer, forums) are primarily for discussing content related to individual courses offered by MongoDB University. Your question, has no relation to the M320 Data Modeling course content.

Secondly, when you ask unrelated questions and someone suggests something, you need to show some initiative by posting what you tried. This is not stackoverflow.com (or any other Tech forums), it’s a discussion board for learning hence the reason why suggestions are made. Therefore, you’ll get better responses when you show initiative.

Lastly, here’s what you’re trying to achieve using the same $lookup I suggested earlier which can be converted to your specific driver:

db.sample_collection1.aggregate([
  {
    $lookup: {
      from: 'sample_collection2',
      let: {localField: '$field2.field4.field6'},
      pipeline: [
        {
          $addFields: {
            id: {$toString: '$_id'},
            localField: { 
              $arrayElemAt: [{ 
                $arrayElemAt: [{ 
                  $arrayElemAt: [ '$$localField', 0 ] 
                }, 0]
              }, 0]
            },
          }
        },
        {
          $match: {
            $expr: {
              $or: [
                {$eq: ['$localField', '$id']},
                {$eq: ['$localField', '$childfield1']}
              ]
            }
          }
        },
        {
          $project: {
            childfield2: '$childfield2',
            childfield3: '$childfield3',
            _id: 0
          }
        }
      ],
      as: 'join'
    }
  },
  {
    $project: {
      'field2.field4.field6': '$join',
      field6Merged: {$mergeObjects: '$join'}
    }
  }
]).pretty()

Result:


Feel free to ask questions for clarity.

NB: You’re better off re-modelling these schemas.

1 Like

Thanks for your time @007_jb. I have one more question I am trying to get only field6 list but projection is not working. can you help me

by using db.collection.find({ “Name”:“L”},{ “newfield”:"$field2.0.field4.0.field6" } );

here using 0 to get first element in list that also getting from field, have to get nested field. In my case nested documents can be more than 10. so using position operator. please provide simple query to get nested field using position of fields

It seems that the projection of multi-nested array fields in find() is very limited, meaning what you’re trying to do is not possible with find().

However, you can $project field6 by utilising the example I gave above which uses aggregate() and arrayElemAt[] with which you can specify the index/position:

db.collection.aggregate([
   {
      $match: {
         'Name': 'L'
      }
   },
   {
      $project: {
         newField: { 
            $arrayElemAt: [{ 
               $arrayElemAt: [{ 
                  $arrayElemAt: [ '$field2.field4.field6', 0 ] 
               }, 0]
            }, 0]
         },
      }
   }
])

Thanks very much @007_jb

Hi @satishreddy,

If you have more queries which are not related to any course content, I would recommend posting here:

Now, I am closing this thread, if you have more questions on the forum, please create a new Topic :slight_smile:

Kanika