加入?来自mongodb集合中数组的数据

时间:2017-08-24 22:34:13

标签: mongodb mongodb-query

我有一个包含另一个集合中对象版本的集合。每个文档都有修改日期和该对象的新版本。

{
  date: "2017-03-10T00:00:00.000",
  user: { 
      _id: 1,
      email: "a@b.com",
      ...
  }
},
{
  date: "2017-04-12T00:00:00.000",
  user: { 
      _id: 1,
      email: "b@c.com",
      ...
  }
},
{
  date: "2017-05-17T00:00:00.000",
  user: { 
      _id: 1,
      email: "c@d.com",
      ...
  }
}

我想知道如何将这些数据转换为这样的数据:

{
   start: "2017-03-10T00:00:00.000", // creation date for the first version
   end: "2017-04-12T00:00:00.000", // creation date for the second version
   user: { 
       _id: 1,
       email: "a@b.com",
       ...
   }
},
{
   start: "2017-04-12T00:00:00.000", // creation date for the second version
   end: "2017-05-17T00:00:00.000", // creation date for the third version
   user: { 
       _id: 1,
       email: "b@c.com",
       ...
   }
},
{
   start: "2017-05-17T00:00:00.000", // creation date for the third version
   end: null,
   user: { 
       _id: 1,
       email: "c@d.com",
       ...
   }
}

我正试图用聚合器来做这件事,但目前我被困了:'(

0 个答案:

没有答案