如何从结果查询中收集所有值mongodb成为数组

时间:2014-11-20 06:32:43

标签: mongodb find

如何从结果查询中收集所有值mongodb成为数组,例如

当我执行此查询" db.organisms.find()"时,我得到的结果如下:

{
    "_id" : ObjectId("8686cdcdc89798797"),
    "name" : "Horse",
    "parent" : "animal",
},
{
    "_id" : ObjectId("90808dsdd55679sss"),
    "name" : "Cow",
    "parent" : "animal",
},
{
    "_id" : ObjectId("908890dsddsd000808"),
    "name" : "Rose",
    "parent" : "flower",
}

查询如何得到这样的结果:

result = ["animal", "flower"]

怎么做?

1 个答案:

答案 0 :(得分:1)

db.collections.distinct()操作应该可以解决问题。

db.organisms.distinct("parent");