我有一个MongoDB集合,其_id由键、值和度量建立。我想找到特定键值对的所有不同度量。如何最好地实现这一点?即,对于以下数据
> db.schedule.find()
{ "_id" : { "key" : "source", "value" : "a", "metric" : "index.references.added.all" }, "nextCheck" : ISODate("2016-07-07T14:36:27.760Z") },
{ "_id" : { "key" : "source", "value" : "a", "metric" : "index.references.added.some" }, "nextCheck" : ISODate("2016-07-07T14:36:27.761Z") },
{ "_id" : { "key" : "source", "value" : "b", "metric" : "index.references.added.all" }, "nextCheck" : ISODate("2016-07-07T14:36:27.760Z") },
{ "_id" : { "key" : "source", "value" : "b", "metric" : "index.references.added.some" }, "nextCheck" : ISODate("2016-07-07T14:36:27.759Z") }
我想按照的思路取得一些成就
db.schedule.distinct("_id.metric", {_id : {"key" : "source", "value" : "a", "metric" : {$regex : "all"}}})
["index.references.added.all", "index.references.added.some"]
目前,这不会返回任何内容。我已经确认
db.schedule.distinct("_id.metric")
["index.references.added.all", "index.references.added.some"]
工作。我做错了什么?
您需要使用点表示法来访问嵌入的字段。
db.schedule.distinct(
"_id.metric",
{ "_id.key": "source", "_id.value" : "a", "_id.metric": /all/ }
)