Dit is iets dat we kunnen doen met behulp van het aggregatieraamwerk en zonder verwerking aan de kant van de klant.
MongoDB 3.4
db.collection.aggregate(
[
{ "$sort": { "_id": 1 } },
{ "$group": {
"_id": "$asin",
"doc": { "$first": "$$ROOT" }
}},
{ "$replaceRoot": { "newRoot": "$doc" } },
{ "$out": "collection" }
]
)
MongoDB-versie <=3.2:
db.collection.aggregate(
[
{ "$sort": { "_id": 1 } },
{ "$group": {
"_id": "$asin",
"doc": { "$first": "$$ROOT" }
}},
{ "$project": {
"asin": "$doc.asin",
"url": "$doc.url",
"image": "$doc.image",
"salesRank": "$doc.salesRank",
"title": "$doc.salesRank",
"brand": "$doc.brand",
"favourite": "$doc.favourite",
"createdAt": "$doc.createdAt",
"updatedAt": "$doc.updatedAt"
}},
{ "$out": "collection" }
]
)