Shilpa
Shilpa

Reputation: 101

Query for Unique Documents in mongo DB collection

I need a query to copy only unique documents from "A" collection to "B" collection. I shall create an index on "B" collection and then copy the unique documents from "A" to "B" collection.

The "A" collection has almost 3671945 documents.

"A" Collection Mongo DB Document Structure:

    {
        "_id" : ObjectId("5d654ed25616a9d461bc0a91"),
        "aC" : "AN",
        "aI" : "ABCD",
        "aN" : "000000000001",
        "aT" : "AB",
        "pD" : "2022-11-07",
        "transaction" : {
            "seqNo" : 1,
            "tC" : "S"
        }
    },
    {
        "_id" : ObjectId("5d654ed25616a9d461bc0a93"),
        "aC" : "AN",
        "aI" : "ABCD",
        "aN" : "000000000001",
        "aT" : "AB",
        "pD" : "2022-11-07",
        "transaction" : {
            "seqNo" : 1,
            "tC" : "S"
        }
    },
    {
        "_id" : ObjectId("5d654ed25616a9d461bc0a92"),
        "aC" : "AN",
        "aI" : "ABCD",
        "aN" : "000000000002",
        "aT" : "AB",
        "pD" : "2022-11-08",
        "transaction" : {
            "seqNo" : 2,
            "tC" : "S"
        }
    }

Now, I shall create my primary index on "B" Collection as follows,

    {
    "aC" : 1,
    "aI" : 1,
    "aN" : 1,
    "aT" : 1,
    "transaction.seqNo" : 1,
    "transaction.tC" : 1,
    "pD" : 1
    }

Could you please help me out to query only unique documents from "A" collection to "B" collection based on Index enabled on "B" collection?

Need help on the query .

Thanks in advance.

Upvotes: 0

Views: 58

Answers (1)

ray
ray

Reputation: 15286

Do a $group with $first to pick one document per grouping. Save $$ROOT and later replace with the saved $$ROOT. Finally use $merge to save to collection B.

db.collection.aggregate([
  {
    "$group": {
      "_id": {
        "aC": "$aC",
        "aI": "$aI",
        "aN": "$aN",
        "aT": "$aT",
        "seqNo": "$transaction.seqNo",
        "tC": "$transaction.tC",
        "pD": "$pD"
      },
      doc: {
        $first: "$$ROOT"
      }
    }
  },
  {
    "$replaceRoot": {
      "newRoot": "$doc"
    }
  },
  {
    "$merge": {
      "into": "B",
      "on": "_id",
      "whenMatched": "replace"
    }
  }
])

Mongo Playgroun

A side note: you may want to perform the insertion before creating index to improve the write speed.

Upvotes: 0

Related Questions