Arun
Arun

Reputation: 25

Storing Aggregation pipeline inside materialized view in Mongo

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,},},
      { $merge: { into: "collection_stg", on: "_id", whenMatched: "replace" , whenNotMatched: "insert",} },
      {$unwind: {path: "$relatonship",},},
      {$group: {_id: "$relatonship.subject",relation: {$addToSet: {predicate: "$relatonship.predicate",object: "$relatonship.object",startDate: "$relatonship.startDate"},},},},
   ] );
};

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

Upvotes: 0

Views: 102

Answers (0)

Related Questions