【问题标题】:mongodb 3.2.6: $lookup and $project rename _id field using mongoidmongodb 3.2.6:$lookup 和 $project 使用 mongoid 重命名 _id 字段
【发布时间】:2018-08-10 13:06:28
【问题描述】:

我有两个收藏书籍和作者。当我在这些集合之间进行查找时,我得到了想要的结果,但我需要将“_id”重命名为“id”。当我重命名这些字段时,作者“_id”被替换为书籍“_id”而不是作者“_id”。请看下文

  Book.collection.aggregate([
  {'$lookup' => {'from' => "authors",'
                localField' => "_id",
                'foreignField' => "book_id",
                'as' => "authors"}}
  ])

结果:

{
  "_id": 1,
  "title": "abc123",
  "isbn": "0001122223334",
  "copies": 5,
  "updated_at": "2018-03-02T09:17:24.546Z",
  "created_at": "2018-03-02T09:17:24.546Z",
  "authors": [
    {
      "_id": 10,
      "first": "a",
      "last": "a",
      "book_id": 1,
      "updated_at": "2018-03-02T09:22:07.115Z",
      "created_at": "2018-03-02T09:22:07.115Z"
    }
  ]
}

我已尝试将 _id 字段重命名为 id

Book.collection.aggregate([
  {'$lookup' => {'from' => "authors",
                'localField' => "_id",
                'foreignField' => "book_id",
                'as' => "authors"}},
  {'$project' => {'id' => '$_id', '_id' => 0, 'title' => 1, "isbn" => 1, "copies" => 1,  "updated_at" => 1, 
                  "authors" => { 'id' => '$_id', 'first' => 1, 'last' => 1, 'book_id' => 1, 'updated_at' => 1}}}

  ])

如果我说在上面的“项目”中

"authors" => { 'id' => '$_id'

那么结果就是

{
  "id": 1,
  "title": "abc123",
  "isbn": "0001122223334",
  "copies": 5,
  "updated_at": "2018-03-02T09:17:24.546Z",
  "created_at": "2018-03-02T09:17:24.546Z",
  "authors": [
    {
      "id": 1,
      "first": "a",
      "last": "a",
      "book_id": 1,
      "updated_at": "2018-03-02T09:22:07.115Z",
      "created_at": "2018-03-02T09:22:07.115Z"
    }
  ]
}

作者的 id 是“1”,而应该是“10”。 请建议我需要如何进行更改

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    试试这个,放松一下,然后在项目管道中给这个$authors._id

    Book.collection.aggregate([
      {'$lookup' => {'from' => "authors",
                    'localField' => "_id",
                    'foreignField' => "book_id",
                    'as' => "authors"}},
      {'$unwind' => '$authors'},
      {'$project' => {'id' => '$authors._id', '_id' => 0, 'title' => 1, "isbn" => 1, "copies" => 1,  "updated_at" => 1, 
                      "authors" => { 'id' => '$_id', 'first' => 1, 'last' => 1, 'book_id' => 1, 'updated_at' => 1}}}
      ])
    

    【讨论】:

    • 书和作者id互换,$authors._id是一个数组。如果有超过 1 个作者,它将包含所有作者的 ID。
    • 所以即使您只想将 _id 更改为 id,您也必须手动添加所有字段?
    【解决方案2】:

    您可以$unwind作者、项目ID,然后分组,或使用$map单独重塑作者。我相信后者应该表现得更好,但它的灵活性有点低,因为你硬编码作者的字段:

    Book.collection.aggregate([
    {$addFields: {id:"$_id", "authors": {$map:{
        input: "$authors",
        as: "a",
        in: {
          "id": "$$a._id",
          "first": "$$a.first",
          "last": "$$a.last",
          "book_id": "$$a.book_id",
          "updated_at": "$$a.updated_at",
          "created_at": "$$a.created_at"
        }
    }}}},
    {$project: {_id:0}}
    ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      • 2021-04-11
      • 2019-05-11
      • 2020-08-07
      • 2021-05-02
      相关资源
      最近更新 更多