close nav

close nav

Thread: How to aggregate a lookup in a nested property

Forum : Basic MongoDB   16 Jul, 2019
User Image
Sriman

Contributor

Joined: 15 Jul, 2016

Points: 1820

Threads: 1920

Posts: 293

How to aggregate a lookup in a nested property

| Quote Date : 16 Jul, 2019    Views:491    

I have a Java Spring Application connected to a mongo db.

In that db, I have two collections:

atas which contains documents with following schema:



{
    ata: string,
    description: string
    }

and

corrections which contains documents with following schema:



{
    //some properties
    predictions: [
        {ata: string, accuracy: float}
    ],
    //other properties
}

I want to "join" both collections on the ata property of the atas collection and the ata property of the predictions property of the corrections property so that I end up with a document with


tags: mongodb

Comments

User Image
Sirisha

Newbie

Joined : 21 Nov, 2018

Points : 100

Threads: 0

Posts: 1

Re: How to aggregate a lookup in a nested property

Reply Date : 9 Dec, 2019

db.atas.aggregate({$lookup:{from:"corrections",localField:"ata",foreignField:"predictions.ata",as:"join"}},{$unwind:"$join"},{$unwind:"$join.predictions"},{$project:{ata:1,description:1,accuracy:"$join.predictions.accuracy",
  equality:{$eq:["$ata","$join.predictions.ata"]}}},{$match:{"equality":true}})

Load More

No More Comments