How to Weigh mongoDB Data? For example, the following collection:
{"id":"1","name":"apple"}
{"id":"2","name":"pear"}
{"id":"3","name":"apple"}
{"id":"4","name":"peach"}
{"id":"5","name":"pear"}
Question:
Is to delete the repetition, the result is like this:
{"id":"1","name":"apple"}
{"id":"2","name":"pear"}
{"id":"4","name":"peach"}
Generally speaking, it is the result of deduplication rather than deletion from the database. To achieve this goal:
db.coll.aggregate([ {$sort: {id: 1}}, {$group: {_id: "$name", doc: {$first: "$$ROOT"}}}, {$replaceRoot: {newRoot: "$doc"}} ]);
If you really want to delete, then all records that are not in this result can be deleted.