1

我快疯了...我来自 sql 世界,这是我第一次真正体验 mongodb。

我有一个给定的 json/object 结构(我知道,这并不完美,但由于现有的数据和其他应用程序,它必须如此),存储在一个以 Restheart 作为 http 前端的 mongodb(v3.4)中。

文件看起来像这样

{
    "_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
}
4

0 回答 0