MongoDB UUID列表与SQL指南列表之间的差异

问题描述 投票:0回答:1

背景:]

我有一个在文档中包含事务处理数据的源MongoDB数据库,并且这些数据库具有标准的“ _id”字段作为索引。

我也有一个SQL SSRS Reporting数据库,用于存储从中创建数据提取的相同事务数据。

要创建摘要的验证部分,是对源数据库和报表数据库之间的记录数进行计数验证。目前,这并不是说我在报表数据库中有一个针对特定客户/日期范围的额外记录。

报告数据库在其名为“ TransactionId”的表中还具有与主键相同的“ _id”字段。

MongoDB数据库和SQL Reporting数据库都还具有CreationTimestamp的概念,这在两个数据库之间在事务级别都是通用的。

问题:]

使用数据库查询,如何在SQL中找到源MongoDB集合中没有的额外记录?

此外,能够以其他方式(在MongoDB中,但在SQL中则不是)能够做到这一点很方便,这可能更常见。

sql mongodb list uuid guid
1个回答
0
投票

我在这里没有得到答案,所以我自己弄清楚了。我将在此处发布解决方案,但我很高兴看到任何替代/更好的解决方案,因为这有点笨拙。

在2个日期之间查找和排序MongoDB UUID

// This should match the “Source count” in my logs
db.getCollection('My_Mongo_Collection')
    .find({"CreationTimestamp" : {$gte : ISODate('2020-02-18T00:00:00.000Z'), $lte : ISODate('2020-02-27T23:59:59.999Z')}})
    .count()

// This will find and sort the “_id” UUIDs of the documents within the date range
var cursor = db.getCollection('My_Mongo_Collection')
    .find({"CreationTimestamp" : {$gte : ISODate('2020-02-18T00:00:00.000Z'), $lte : ISODate('2020-02-27T23:59:59.999Z')}}, {"_id": 1})
    .sort({"CreationTimestamp" : -1})

// This will format the resulting UUIDs into a format that SQL expects for insertion into a temporary table
cursor.forEach(function(user){
    var str = user._id.valueOf()+",";
    print(
        str.replace("UUID(\"", "('")
          .replace("\"),", "'),"));
});

在SQL中查找多余的/缺少的UUID

--DROP TABLE #temp

-- This will create a temporary table in memory
CREATE TABLE #temp (
id VARCHAR(50))

-- Insert the formatted UUIDs from the Mongo script query. This will create a temporary table in memory
INSERT INTO #temp
VALUES 
('238832d1-d0ab-4dc3-80cb-ab6d00b811ae'),
('00ceef1e-1b9e-4f1f-a8d8-ab6d00b811ae'),
...

-- When inserting more than 1000 records the following error will be seen. 
Msg 10738, Level 15, State 1, Line 2011
The number of row value expressions in the INSERT statement exceeds the maximum allowed number of 1000 row values.

-- Click on the error and continue like so, deleting the comma above the error and creating a second insert statement for the second 1000 records, run the query again to get the next 1000 error and keep repeating this cycle until you get to the last record:
('7c505e5e-41b4-4e40-9044-ab6d00b811ae'),
('5f197206-b218-4785-9f0f-ab6d00b811ae')

INSERT INTO #temp
VALUES 
('a9d174e9-a30a-42b5-8815-ab6d00b811ae'),
('72c728e6-0c57-4109-89ca-ab6d00b811ae')
. . .

-- Delete the trailing comma off the last record like so and run the CREATE and INSERT statements.
('672af0f6-d643-4101-acb3-ab6500fc539c'),
('78bf2c9f-20b4-4fa2-8c06-ab6500fc539c')

在MongoDB中找到更多内容,但在SQL中找不到

-- Find extra records in Mongo that are not in SQL
SELECT * FROM #temp 
WHERE id NOT IN 
       (SELECT TransactionId FROM My_Reporting_Table)

在SQL中找到其他内容,但不在MongoDB中找到

-- Find extra records in SQL that are not in Mongo
SELECT * FROM My_Reporting_Table
WHERE TradeBookedId NOT IN
       (SELECT id FROM #temp) AND CreationTimestamp BETWEEN '2020-02-19 00:00:00.000' AND '2020-02-28 23:59:59.999'

[注意:Mongo区分大小写,仅使用小写的UUID,SQL不区分大小写,可以使用小写或大写的UUID。

UUID和GUID之间没有区别Is there any difference between a GUID and a UUID?

© www.soinside.com 2019 - 2024. All rights reserved.