【问题标题】:Restheart query for an nested array subdocument嵌套数组子文档的 Restheart 查询
【发布时间】:2017-09-13 11:13:07
【问题描述】:

我正在使用 mongodb 和 restheart。

在我的 nosql 数据库中,我有一个具有这种结构的独特文档:

{
"_id": "docID",
"users": [
            {
             "userID": "12",                 
             "elements": [
                         {
                          "elementID": "1492446877599",
                          "events": [
                                     {
                                      "event1": "one"
                                     },   
                                     {                                       
                                      "event2": "two",
                                      }
                                     ]
                           }
               },
              {
             "userID": "11",                 
             "elements": [
                         {
                          "elementID": "14924",
                          "events": [
                                     {
                                      "event1": "one"
                                     },   
                                     {                                       
                                      "event2": "two",
                                      }
                                     ]
                           }
               }  

              ]  
}

如何构建 url-query 以获取 id 为 11 的用户?

使用 mongo shell 应该是这样的:

db.getCollection('collection').find({},{'users':{'$elemMatch':{'userID':'12'}}}).pretty()

我在 restheart 上找不到类似的东西。

有人可以帮我吗?

使用这个

http://myHost:port/documents/docID?filter={%27users%27:{%27$elemMatch%27:{%27userID%27:%2712%27}}}

restheart 将所有文档返回给我:用户 ID 11 和 12。

【问题讨论】:

    标签: arrays mongodb subdocument restheart


    【解决方案1】:

    您的请求针对的是文档资源,即 URL 是 http://myHost:port/documents/docID

    filter查询参数适用于收集请求,即http://myHost:port/documents等URL

    在任何情况下你都需要投影(keys 查询参数)来限制返回的属性。

    您应该使用$elementMatch projection operator通过以下请求(我没有尝试过)来实现它:

    http://myHost:port/documents?keys={"users":{"$elemMatch":{"userID":"12"}}}

    【讨论】:

      猜你喜欢
      • 2020-11-28
      • 2017-03-22
      • 2019-10-09
      • 1970-01-01
      • 2013-08-28
      • 2021-07-21
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      相关资源
      最近更新 更多