MongoDB Ignoring duplicate documents using unique key in aggregate
You can use $first
to get the first element of each group. You can then use $group
again to sum by language
.
{
"$group": {
"_id": {
"language": "$language",
"sId": "$sId"
},
"count": {
"$first": {
"$sum": "$count"
}
}
}
}
https://mongoplayground.net/p/3_RjSt1wtRS
How to remove duplicates based on a key in Mongodb?
This answer is obsolete : the dropDups
option was removed in MongoDB 3.0, so a different approach will be required in most cases. For example, you could use aggregation as suggested on: MongoDB duplicate documents even after adding unique key.
If you are certain that the source_references.key
identifies duplicate records, you can ensure a unique index with the dropDups:true
index creation option in MongoDB 2.6 or older:
db.things.ensureIndex({'source_references.key' : 1}, {unique : true, dropDups : true})
This will keep the first unique document for each source_references.key
value, and drop any subsequent documents that would otherwise cause a duplicate key violation.
Important Note: Any documents missing the source_references.key
field will be considered as having a null value, so subsequent documents missing the key field will be deleted. You can add the sparse:true
index creation option so the index only applies to documents with a source_references.key
field.
Obvious caution: Take a backup of your database, and try this in a staging environment first if you are concerned about unintended data loss.
How to stop insertion of Duplicate documents in a mongodb collection
Don't use insert.
Use update with upsert=true
. Update will look for the document that matches your query, then it will modify the fields you want and then, you can tell it upsert:True if you want to insert if no document matches your query.
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
So, for your example, you could use something like this:
db.collection.update(doc, doc, {upsert:true})
Related Topics
How to Use PHP Namespaces With Autoload
Netbeans Shows "Waiting For Connection (Netbeans-Xdebug)"
How to Change PHP Version in Htaccess in Server
PHP File Cannot Enter Some Part of Code
Laravel Migration: Unique Key Is Too Long, Even If Specified
How to Remove HTML Special Chars
Checking If a Variable Is an Integer in PHP
File_Get_Contents =≫ PHP Fatal Error: Allowed Memory Exhausted
Call to Undefined Function Curl_Init().
PHP Curl Not Working - Wamp on Windows 7 64 Bit
How to Override the Path of PHP to Use the Mamp Path
The Openssl Extension Is Required For Ssl/Tls Protection
Explode String by One or More Spaces or Tabs
Laravel - Eloquent or Fluent Random Row