我疯了......我来自sql世界,这是我第一次使用mongodb体验。
我有一个给定的json / object结构(我知道,这不是完美的,但由于现有数据和其他应用程序必须如此),存储在mongodb(v3.4)中,Restheart作为http前端。
文件看起来像这样
{
"_id" : ObjectId("5855cbc9fc3baea81e937261"),
"_etag" : ObjectId("5855cbc99b971700050d8adc"),
"log" : [
"858489b087f7472dbb8d1012dee4cd5d.d",
NumberLong("12345678901234"),
{
"ext" : {
"text" : "someone did something at somewhere (some street 123) +38 USD",
"markup" : [
[
"user",
{
"plain" : "someone",
"team" : "master"
}
], [
"TEXT",
{
"plain" : " did something at "
}
], [
"location",
{
"name" : "somewhere",
"plain" : "some street 123",
"team" : "master"
}
], [
"TEXT",
{
"plain" : "38"
}
], [
"TEXT",
{
"plain" : "USD"
}
]
],
"category" : 1,
"team" : "master"
}
}
]
}
我想获得一个usern.plain名称的明确列表。从理论上讲,db.logs.distinct("log.2.ext.markup.0.1.plain")
会完全符合我的需要。但据我所知,没有办法将db.distinct与Restheart一起使用。我尝试使用视图,但似乎,我也不能在视图中使用db.distinct。
这是我的实验......
{ "aggrs" : [
{ "stages" : [
{ "_$project" : { "user" : "$log.2.ext.markup.0.1.plain"}},
{ "_$unwind" : "$user"},
{ "_$unwind" : "$user"},
{ "_$unwind" : "$user"},
{ "_$unwind" : "$user"},
{ "_$unwind" : "$user"},
{ "_$unwind" : "$user"},
{ "_$group" : { "_id" : "$user"}}
],
"type" : "pipeline",
"uri" : "unique_users1"
},
{ "stages" : [
{ "_$match": { "log": { "_$exists": true, "_$ne": null }}},
{ "_$unwind" : "$log"},
{ "_$unwind" : "$log.2"},
{ "_$unwind" : "$log.2.ext"},
{ "_$unwind" : "$log.2.ext.markup"},
{ "_$unwind" : "$log.2.ext.markup.0"},
{ "_$unwind" : "$log.2.ext.markup.0.1"},
{ "_$group" : { "_id" : "$log.2.ext.markup.0.1.plain"}}
],
"type" : "pipeline",
"uri" : "unique_users2"
},
{ "stages" : [
{ "_$match" : {"log" : { "_$exists" : true }}},
{ "_$replaceRoot" : { "newRoot" : { "user": "$log.2.ext.markup.0.1.plain"}}}
],
"type" : "pipeline",
"uri" : "unique_users3"
},
{ "stages" : [
{ "_$group" : { "_id" : 1 , "users" : { "_$addToSet" : "$log.2.ext.markup.0.1.plain"}}}
],
"type" : "pipeline",
"uri" : "unique_users4"
}
]}
但结果是......没什么或几乎没有什么
{
"_embedded": {
"rh:result": [
{
"_id": 1,
"users": [
[]
]
}
]
},
"_returned": 1,
"_size": 1,
"_total_pages": 1
}