拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
雷雷
> db.test.find() { "_id" : ObjectId("55de6075024f45b4947d4cc3"), "name" : "zhangsan", "sex" : "FEMALE", "age" : 12 } { "_id" : ObjectId("55de6076024f45b4947d4cc4"), "name" : "zhangsan", "sex" : "MALE", "age" : 16 } { "_id" : ObjectId("55de6076024f45b4947d4cc5"), "name" : "lisi", "sex" : "FEMALE", "age" : 20 } { "_id" : ObjectId("55de6076024f45b4947d4cc6"), "name" : "zhangsan", "sex" : "FEMALE", "age" : 25 } { "_id" : ObjectId("55de6076024f45b4947d4cc7"), "name" : "lisi", "sex" : "FEMALE", "age" : 36 } { "_id" : ObjectId("55de6077024f45b4947d4cc8"), "name" : "zhangsan", "sex" : "FEMALE", "age" : 28 } > db.test.aggregate([ ... { $group: { _id: { name: "$name", sex: "$sex" } } } ... ]) { "_id" : { "name" : "lisi", "sex" : "FEMALE" } } { "_id" : { "name" : "zhangsan", "sex" : "MALE" } } { "_id" : { "name" : "zhangsan", "sex" : "FEMALE" } } > db.test.distinct("name"); [ "zhangsan", "lisi" ] > db.test.distinct("name","sex"); [ "zhangsan", "lisi" ] > db.test.distinct("name","sex","age"); [ "zhangsan", "lisi" ]
樓上的方法是正確的,mongoDB確實不支援組合去重,mongo官網提供的範例也只是對單一欄位去重。
官網範例: http://docs.mongodb.org/manual/core/single-purpose-aggregation/
雷雷
雷雷
樓上的方法是正確的,mongoDB確實不支援組合去重,mongo官網提供的範例也只是對單一欄位去重。
官網範例: http://docs.mongodb.org/manual/core/single-purpose-aggregation/