Jonathan
Jonathan

Reputation: 41

Mongodb graphLookup cond into connectToField

Its posible add $cond into $connectToField example

{ $graphLookup: { from: 'samecollection', startWith: '$myid', connectFromField: 'myid', connectToField: { $cond: { true , 'myidsRelated.id', 'newRelated.id'} }, as: 'elements', }, },

Upvotes: 0

Views: 120

Answers (1)

MikkolidisMedius
MikkolidisMedius

Reputation: 4842

No, it is not possible. The connectToField parameter only allows you to specify the name of the field to join to. It does not support expressions (and this is the reason why the field name is specified without the $ prefix).

However, there is a restrictSearchWithMatch option that you can use to filter the documents you want to participate in the lookup. For example:

{
  $graphLookup: {
    from: "somecollection",
    startWith: "$myid",
    connectFromField: "myid",
    connectToField: "newRelated.id",
    as: "elements",
    restrictSearchWithMatch: { "newRelated.id": { $exists: true } }
  }
}

Upvotes: 1

Related Questions