mongo数据库$out输出覆盖原集合

数据库版本:4.2.8

操作系统:ubuntu20

mongo aggregate 中 $out输出可以将原集合覆盖。

问题复现:

1、写入测试数据

rs0:PRIMARY> use ceshi
rs0:PRIMARY> db.t1.insert({id:1})
rs0:PRIMARY> db.t1.insert({id:2})
rs0:PRIMARY> db.t1.insert({id:3})
rs0:PRIMARY> db.t1.insert({id:4})

2、执行 aggregate 命令并将结果输出到原文件

rs0:PRIMARY> db.t1.aggregate([
...   {
...     $match: {
...        id:1
...     }
...   },
...   {
...     $addFields: {
...       updateTime: "$createTime"
...     }
...   },
...   {
...     $out: "t1"
...   }
... ]);

3、查询结果

rs0:PRIMARY> db.t1.find()
{ "_id" : ObjectId("65321108cd8951f765724f82"), "id" : 1 }

4、 分析 oplog

4.1 首先创建了 t1 集合,并写入 1,2,3,4 数据
4.2 执行聚合操作并将结果输出到 t1 集合时, 数据库首先创建了一个 eshi.tmp.agg_out.2 集合,然后将结果 id:1 写入到这个临时集合中,最后再将临时集合重命名为 t1 集合,完美的将之前 t1 集合进行了覆盖。
但是如果在 mongo shell 中手动执行 db.xx.renameCollection('t1'); 会报错目标集合已经存在。

{
        "ts" : Timestamp(1697779976, 1),
        "t" : NumberLong(1),
        "h" : NumberLong(0),
        "v" : 2,
        "op" : "c",
        "ns" : "ceshi.$cmd",
        "ui" : UUID("dcf58625-df4f-4576-8037-39c8a615ee44"),
        "wall" : ISODate("2023-10-20T05:32:57.009Z"),
        "o" : {
                "create" : "t1",
                "idIndex" : {
                        "v" : 2,
                        "key" : {
                                "_id" : 1
                        },
                        "name" : "_id_",
                        "ns" : "ceshi.t1"
                }
        }
}
{
        "ts" : Timestamp(1697779977, 1),
        "t" : NumberLong(1),
        "h" : NumberLong(0),
        "v" : 2,
        "op" : "i",
        "ns" : "ceshi.t1",
        "ui" : UUID("dcf58625-df4f-4576-8037-39c8a615ee44"),
        "wall" : ISODate("2023-10-20T05:32:57.009Z"),
        "o" : {
                "_id" : ObjectId("65321108cd8951f765724f82"),
                "id" : 1
        }
}
{
        "ts" : Timestamp(1697779978, 1),
        "t" : NumberLong(1),
        "h" : NumberLong(0),
        "v" : 2,
        "op" : "i",
        "ns" : "ceshi.t1",
        "ui" : UUID("dcf58625-df4f-4576-8037-39c8a615ee44"),
        "wall" : ISODate("2023-10-20T05:32:58.690Z"),
        "o" : {
                "_id" : ObjectId("6532110acd8951f765724f83"),
                "id" : 2
        }
}
{
        "ts" : Timestamp(1697779980, 1),
        "t" : NumberLong(1),
        "h" : NumberLong(0),
        "v" : 2,
        "op" : "i",
        "ns" : "ceshi.t1",
        "ui" : UUID("dcf58625-df4f-4576-8037-39c8a615ee44"),
        "wall" : ISODate("2023-10-20T05:33:00.155Z"),
        "o" : {
                "_id" : ObjectId("6532110ccd8951f765724f84"),
                "id" : 3
        }
}
{
        "ts" : Timestamp(1697779981, 1),
        "t" : NumberLong(1),
        "h" : NumberLong(0),
        "v" : 2,
        "op" : "i",
        "ns" : "ceshi.t1",
        "ui" : UUID("dcf58625-df4f-4576-8037-39c8a615ee44"),
        "wall" : ISODate("2023-10-20T05:33:01.853Z"),
        "o" : {
                "_id" : ObjectId("6532110dcd8951f765724f85"),
                "id" : 4
        }
}
{
        "ts" : Timestamp(1697780014, 1),
        "t" : NumberLong(1),
        "h" : NumberLong(0),
        "v" : 2,
        "op" : "c",
        "ns" : "ceshi.$cmd",
        "ui" : UUID("60c2d418-428f-4b53-9cae-34a89de31261"),
        "wall" : ISODate("2023-10-20T05:33:34.283Z"),
        "o" : {
                "create" : "tmp.agg_out.2",
                "temp" : true,
                "idIndex" : {
                        "v" : 2,
                        "key" : {
                                "_id" : 1
                        },
                        "name" : "_id_",
                        "ns" : "ceshi.tmp.agg_out.2"
                }
        }
}
{
        "ts" : Timestamp(1697780014, 2),
        "t" : NumberLong(1),
        "h" : NumberLong(0),
        "v" : 2,
        "op" : "i",
        "ns" : "ceshi.tmp.agg_out.2",
        "ui" : UUID("60c2d418-428f-4b53-9cae-34a89de31261"),
        "wall" : ISODate("2023-10-20T05:33:34.284Z"),
        "o" : {
                "_id" : ObjectId("65321108cd8951f765724f82"),
                "id" : 1
        }
}
{
        "ts" : Timestamp(1697780014, 3),
        "t" : NumberLong(1),
        "h" : NumberLong(0),
        "v" : 2,
        "op" : "c",
        "ns" : "ceshi.$cmd",
        "ui" : UUID("60c2d418-428f-4b53-9cae-34a89de31261"),
        "o2" : {
                "numRecords" : 4
        },
        "wall" : ISODate("2023-10-20T05:33:34.284Z"),
        "o" : {
                "renameCollection" : "ceshi.tmp.agg_out.2",
                "to" : "ceshi.t1",
                "stayTemp" : false,
                "dropTarget" : UUID("dcf58625-df4f-4576-8037-39c8a615ee44")
        }
}


posted on 2023-10-20 18:08  柴米油盐酱醋  阅读(91)  评论(0编辑  收藏  举报

导航