收藏
回答

如何将子集合提到上层?

//查询语句
db.collection('ProgramOfPerson').aggregate()
.match(_.and([{
  _openid: '*'
}, {
  like: 1
}]))
.project({
  programid: 1
})
.lookup({
  from: 'Program',
  let: {
    id: '$programid'
  },
  pipeline: $.pipeline()
    .match(_.expr($.and([
    $.eq(['$_id', '$$id']),
    ])))
    .project({
      _id:0,
      title: 1,
      datetime: 1,
      content: $.substrCP(['$content', 0, 100])
    })
    .done(),
    as: 'a'
  })
  .end()


[{
    "_id": "*",
    "programid": "*",
    "a": [
      {
        "datetime": "2022/11/22 12:33:26",
        "title": "24",
        "content": "24\n"
      }
    ]
  }]

将上面的转为下面的

[{
    "_id": "*",
    "programid": "*",
    "datetime": "2022/11/22 12:33:26",
    "title": "24",
    "content": "24\n"
  }]


最后一次编辑于  2022-11-29
回答关注问题邀请回答
收藏

2 个回答

登录 后发表内容