mongodb 从mongo DB集合中的对象数组查询唯一文档

cbjzeqam  于 2022-11-22  发布在  Go
关注(0)|答案(1)|浏览(129)

我需要一个查询,以消除重复的文件从“A”收集。一旦重复被删除,然后我将创建一个索引“A”收集。
“A”集合有将近132920个文档。
请查找“A”系列Mongo DB文档结构,

/*1 */
    {
        "_id" : ObjectId("5d654ed25616a9d461bc0aa7"),
        "aC" : "AB",
        "aI" : "ABCD",
        "aN" : "000000002001",
        "aT" : "CA",
        "bID" : NumberLong(0),
        "pD" : "2019-06-18",
        "transactions" : [ 
            {
                "seqNo" : 1,
                "tC" : "S"
            }, 
            {
                "seqNo" : 2,
                "tC" : "S"
            },
            {
                "seqNo" : 2,
                "tC" : "S"
            },
            {
                "seqNo" : 1,
                "tC" : "S"
            },
            {
                "seqNo" : 3,
                "tC" : "S"
            }
        ]
    }

/*2*/
{
        "_id" : ObjectId("5d654ed25616a9d461bc0aa8"),
        "aC" : "AB",
        "aI" : "ABCD",
        "aN" : "000000002002",
        "aT" : "CA",
        "bID" : NumberLong(0),
        "pD" : "2019-06-18",
        "transactions" : [ 
            {
                "seqNo" : 1,
                "tC" : "S"
            }, 
            {
                "seqNo" : 2,
                "tC" : "S"
            },
            {
                "seqNo" : 2,
                "tC" : "S"
            },
            {
                "seqNo" : 1,
                "tC" : "S"
            },
            {
                "seqNo" : 3,
                "tC" : "S"
            }
        ]
    }

预期输出:

/*1*/
{
        "_id" : ObjectId("5d654ed25616a9d461bc0aa7"),
        "aC" : "AB",
        "aI" : "ABCD",
        "aN" : "000000002001",
        "aT" : "CA",
        "bID" : NumberLong(0),
        "pD" : "2019-06-18",
        "transactions" : [ 
            {
                "seqNo" : 1,
                "tC" : "S"
            }, 
            {
                "seqNo" : 2,
                "tC" : "S"
            },
            {
                "seqNo" : 3,
                "tC" : "S"
            }
        ]
    }

/*2*/
{
        "_id" : ObjectId("5d654ed25616a9d461bc0aa8"),
        "aC" : "AB",
        "aI" : "ABCD",
        "aN" : "000000002002",
        "aT" : "CA",
        "bID" : NumberLong(0),
        "pD" : "2019-06-18",
        "transactions" : [ 
            {
                "seqNo" : 1,
                "tC" : "S"
            }, 
            {
                "seqNo" : 2,
                "tC" : "S"
            },
            {
                "seqNo" : 3,
                "tC" : "S"
            }
        ]
    }

在从“事务数组”中删除重复项后,我将在“A”集合上创建我的主索引,如下所示,

{
"aC" : 1.0,
"aI" : 1.0,
"aN" : 1.0,
"aT" : 1.0,
"bID" : 1.0,
"pD" : 1.0

}
你能帮我从“A”集合中删除重复的文档吗?这样我就可以在“A”集合上启用索引了。
需要有关查询的帮助。
先谢谢你了。这会很有帮助的。

2guxujil

2guxujil1#

这里有一个方法可以做到这一点。

db.collection.update({
  "transactions": {"$type": "array"}
},
[
  {
    "$set": {
      "transactions": {"$setUnion": "$transactions"}
    }
  }
],
{"multi": true}
)

mongoplayground.net上试试。

编辑

我不知道这是一个mongoplayground.net还是MongoDB“oddity”/bug,但我怀疑有什么东西对"transactions"数组对象中的字段进行了“重新排序”。
这里有一个“变通办法”,重写"$setUnion"之前的每个元素。

db.collection.update({
  "transactions": {"$type": "array"}
},
[
  {
    "$set": {
      "transactions": {
        "$setUnion": {
          "$map": {
            "input": "$transactions",
            "as": "xaction",
            "in": {
              "seqNo": "$$xaction.seqNo",
              "tC": "$$xaction.tC"
            }
          }
        }
      }
    }
  }
],
{"multi": true}
)

mongoplayground.net上试试。

相关问题