0

I am quite new to Mongo Coming from Sql background.

I have a aggregation on collection_1 which involves $graphlookup (given below)

[
  {
    $match: {
      $or: [
        {
          objStatus: "NEW",
        },
        {
          objStatus: "UNCHANGED",
        },
        {
          relStatus: "NEW",
        },
        {
          relStatus: "UNCHANGED",
        },
        {
          subStatus: "NEW",
        },
        {
          subStatus: "UNCHANGED",
        },
      ],
    },
  },
  {
    $graphLookup: {
      from: "Collection_1",
      startWith: "$predicate",
      connectFromField: "subject",
      connectToField: "predicate",
      as: "relatonship",
      maxDepth: 0,
    },
  },
  {
    $unwind: {
      path: "$relatonship",
    },
  },
  {
    $group: {
      _id: "$relatonship.subject",
      relation: {
        $addToSet: {
          predicate: "$relatonship.predicate",
          object: "$relatonship.object",
          startDate: "$relatonship.startDate"
        },
      },
    },
  },
]

I am quite new to Mongo Coming from Sql background.

I have a aggregation on collection_1 which involves $graphlookup (given below)

[
  {
    $match: {
      $or: [
        {
          objStatus: "NEW",
        },
        {
          objStatus: "UNCHANGED",
        },
        {
          relStatus: "NEW",
        },
        {
          relStatus: "UNCHANGED",
        },
        {
          subStatus: "NEW",
        },
        {
          subStatus: "UNCHANGED",
        },
      ],
    },
  },
  {
    $graphLookup: {
      from: "Collection_1",
      startWith: "$predicate",
      connectFromField: "subject",
      connectToField: "predicate",
      as: "relatonship",
      maxDepth: 0,
    },
  },
  {
    $unwind: {
      path: "$relatonship",
    },
  },
  {
    $group: {
      _id: "$relatonship.subject",
      relation: {
        $addToSet: {
          predicate: "$relatonship.predicate",
          object: "$relatonship.object",
          startDate: "$relatonship.startDate"
        },
      },
    },
  },
]

Initally We saved the above aggregation in standard view but facing lot of cpu issues and read issue

I came across On-Demand Materialized View https://www.mongodb.com/docs/manual/core/materialized-views/#additional-information

I am quite new to Mongo Coming from Sql background.

I have a aggregation on collection_1 which involves $graphlookup (given below)

[
  {
    $match: {
      $or: [
        {
          objStatus: "NEW",
        },
        {
          objStatus: "UNCHANGED",
        },
        {
          relStatus: "NEW",
        },
        {
          relStatus: "UNCHANGED",
        },
        {
          subStatus: "NEW",
        },
        {
          subStatus: "UNCHANGED",
        },
      ],
    },
  },
  {
    $graphLookup: {
      from: "Collection_1",
      startWith: "$predicate",
      connectFromField: "subject",
      connectToField: "predicate",
      as: "relatonship",
      maxDepth: 0,
    },
  },
  {
    $unwind: {
      path: "$relatonship",
    },
  },
  {
    $group: {
      _id: "$relatonship.subject",
      relation: {
        $addToSet: {
          predicate: "$relatonship.predicate",
          object: "$relatonship.object",
          startDate: "$relatonship.startDate"
        },
      },
    },
  },
]

Initally We saved the above aggregation in standard view but facing lot of cpu issues and read issue

I came across On-Demand Materialized View https://www.mongodb.com/docs/manual/core/materialized-views/#additional-information

I framed my logic as :

 updateRelations = function(creationDate) {
       db.Collection_1.aggregate( [
         { $match: { date: { $eq: creationDate } } },
          {$match: {$or: [{objStatus: "NEW",},{objStatus: "UNCHANGED",},{relStatus: "NEW",},{relStatus: "UNCHANGED",},{subStatus: "NEW",},{subStatus: "UNCHANGED",},],},},
          {$graphLookup: {from: "SourceRelationship_DEV",startWith: "$predicate",connectFromField: "subject",connectToField: "predicate",as: "relatonship",maxDepth: 0,},},
          
          {$unwind: {path: "$relatonship",},},
          {$group: {_id: "$relatonship.subject",relation: {$addToSet: {predicate: "$relatonship.predicate",object: "$relatonship.object",startDate: "$relatonship.startDate"},},},},
{ $merge: { into: "collection_stg", on: "_id", whenMatched: "replace" , whenNotMatched: "insert",} }
       ] );
    };

When tried it running from mongo shell the query is getting executing but "collection_stg" is not being created

Please let me know where I am missing

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.