如何使用 mongoose 创建 mongodb 视图

bri*_*lov 5 mongoose

我有一个集合,它使用聚合来计算基于其他字段的字段,我想将其转换为视图。我将如何使用猫鼬来实现这个?

Red*_*gon 5

使用:

MyModel.connection.db.createCollection('myViewName', { 
  viewOn: 'existingCollection', 
  pipeline: [/* aggregation pipeline here */] 
});
Run Code Online (Sandbox Code Playgroud)

来源: https: //github.com/Automattic/mongoose/issues/5694

包含示例的视图的完整文档位于:https://docs.mongodb.com/manual/reference/method/db.createView/