The use of distinct in mongoDB
Requirements: query a type in mongodb (possibly millions of pieces of data in the database) to use as a drop-down list query.
This can be achieved with db.getCollection ('order'). Distinct ("typeName") in the tool.
Java Code:
MongoCollection coll = this.client.getDatabase (this.databaseName) .getCollection ('order')
/ / get the getCollection table name of the mongodb database connection
/ / this.databaseName reads the dynamic configuration directly from the configuration file, that is, the database name
Iterable iterable = coll.distinct ('typeName',String)
/ / what is the value type of String query? since this query is in Chinese characters, it is String.
MongoCursor it = iterable.iterator ()
While (it.hasNext ()) {
String typeName = it.next () .toString ()
System.out.pringln (typeName)
}
/ * MongoCollection: com.mongodb.client.MongoCollection Document:org.bson.Document implements Map, Serializable, Bson * * /