mongodb USES the simple method of distinct deduplication

  • 2020-05-24 06:25:01
  • OfStack

The destinct command of MongoDB is to get a list of different values in a particular field. This command applies to plain fields, array fields, and array embedded documents.

mongodb distinct statement:


db.users.distinct('last_name' )

Equivalent to the SQL statement:


select DISTINCT last_name from users

Represents the return of a different set of records based on the specified field.
1 simple example:


// 
> db.addresses.insert({"zip-code": 10010}) 
> db.addresses.insert({"zip-code": 10010}) 
> db.addresses.insert({"zip-code": 99701}) 
 
> // shell helper: 
> db.addresses.distinct("zip-code"); 
[ 10010, 99701 ] 
 
> // running as a command manually: 
> db.runCommand( { distinct: 'addresses', key: 'zip-code' } ) 
{ "values" : [ 10010, 99701 ], "ok" 
// 
> db.comments.save({"user": {"points": 25}}) 
> db.comments.save({"user": {"points": 31}}) 
> db.comments.save({"user": {"points": 25}}) 
 
> db.comments.distinct("user.points"); 
[ 25, 31 ]

That's all for this article, I hope you enjoy it.


Related articles: