Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The use of distinct in mongoDB

Shulou Source: shulou.com Published: 2022-06-03 05:08:04 11月25日 Update

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 * * /

Tags: Data query database type configuration ten thousand items use code dynamic name tools files requirements Chinese characters drop-down Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Xiaomi Microsoft macOS vpn