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

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

时间:2023-10-20 18:25:19浏览次数:28  
标签:... ns mongo 数据库 ceshi t1 NumberLong id 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")
        }
}


标签:...,ns,mongo,数据库,ceshi,t1,NumberLong,id,out
From: https://www.cnblogs.com/nanxiang/p/17777737.html

相关文章

  • mysql数据库帮助类
    usingMySql.Data.MySqlClient;usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceOA{classMySQLHelper{///<summary>///数据库位置......
  • [CERC2014] Outer space invaders
    题目描述Thealiensfromouterspacehave(finally!)invadedEarth.Defendyourself,orbedisintegrated!Orassimilated.Oreaten.Wearenotyetsure.Thealiensfollowaknownattackpattern.Therearennattackers,thei−thi−thoneappearsattimeaiai......
  • mongo 数据库
    在MongoDB中进行数据库操作的基本命令如下:创建数据库:MongoDB使用时会自动创建数据库,只需通过插入文档或创建集合来创建数据库。切换数据库:使用use<database-name>命令可以切换到指定的数据库,如果该数据库不存在,MongoDB会在插入文档或创建集合时自动创建它。删除数据......
  • Example: Pandas Excel output with column formatting pandas 对excel 列做格式处理
    AnexampleofconvertingaPandasdataframetoanExcelfilewithcolumnformatsusingPandasandXlsxWriter.Itisn’tpossibletoformatanycellsthatalreadyhaveaformatsuchastheindexorheadersoranycellsthatcontaindatesordatetimes.Note:......
  • Redis的速度不够用?为什么你应该考虑使用 KeyDB,一个更快、更强大、更灵活的开源数据库
    你是否正在使用 Redis 作为您的数据结构存储,享受它的高性能、高可用的特性?如果是这样,那么你可能会对 KeyDB 感兴趣。什么是 KeyDB?KeyDB 一个由 Snap 提供支持、专为扩展而构建的开源数据库。它是 Redis 的高性能分支,专注于多线程、内存效率和高吞吐量。KeyDB 采用 M......
  • Windows Python 访问达梦数据库(环境配置)
    WindowsPython访问达梦数据库(环境配置) 一、前提条件本篇博客以访问本地达梦数据库(DM8)为基础进行演示。(前提:本地已经安装了DM8数据库!)关于Windows安装达梦数据库,请参考博客:Windows安装达梦数据库关于Docker安装达梦数据库,请参考博客:Docker安装达梦数据库关于JD......
  • Android入门教程 | DrawerLayout 侧滑栏
    DrawerLayout是实现了侧滑菜单效果的控件。DawerLayout分为侧边菜单和主内容区两部分:主内容区要放在侧边菜单前面,还有就是主内容区最好以DrawerLayout最好为界面的根布局,否则可能会出现触摸事件被屏蔽的问题。侧滑菜单部分的布局必须设置layout_gravity属性,表示侧滑菜单是在......
  • jfinal框架下,连接国产达梦数据库,抛出SocketTimeoutException异常
    公司为政府开发项目,主框架选择springboot,orm框架使用jfinal。数据库为国产达梦数据库写统计类服务时,通常sql运行时间会比较久,超过10s的sql一定会报SocketTimeoutException异常 尝试使用原生jdbc创建连接,运行sql毫无问题。遂检查连接池设置。jfinal使用druid连接池网上搜索......
  • C#调用数据库数据时,突然显示表名无效
    问题描述问题解决调用表时,前面加上数据库名和类似于dbo的那个东西就行啦!解决啦:......
  • 亚马逊Dynamo数据库解读(英文版)
    最近看了亚麻的Dynamo,个人认为其中alwayswriteable的业务目标,对于DHT,vectorclock,merkeltree的应用,包括对于一致性和高可用的权衡(基于CAP猜想,实现默认保证分区容错,因此二选一)等都很有意思。建议参考原论文食用。Whatistheproblemthatthispapertriestosolve?Howwould......