【问题标题】:Retrieve only matched sub document from MongoLab/MongoDB through http/REST通过 http/REST 从 MongoLab/MongoDB 中仅检索匹配的子文档
【发布时间】:2016-02-16 18:40:31
【问题描述】:

是否可以使用 REST HTTP 接口从 MongoDB 集合中与指定查询匹配的对象数组中仅检索子文档。

已经有很多类似问题的答案了,但这些都是使用 MongoDB 驱动程序,而不是 REST/HTTP

有可能吗?

样本数据

[ { “姓名”:“保罗”, “地址”:[ { "addressline1":"1 号街道", “城市”:“德里” }, { "addressline1":"4 号街道", “城市”:“钦奈” } ] }, { “名称”:“玫瑰”, “地址”:[ { "addressline1":"5 号街道", “城市”:“德里” }, { "addressline1":"7 号街道", “城市”:“钦奈” } ] } ]

从上面的数据我只需要得到一个地址,'name'匹配'Paul'和'city'匹配'Delhi'如下

{ "addressline1":"1 号街道", “城市”:“德里” }

我有以下查询要在子文档中搜索。但它会返回包含此子文档查询的子文档数组中的所有元素。

https://api.mongolab.com/api/1/databases/[mydb]/collections/[mycollection? apiKey=[myAPIKey] &q={"name":"Paul","addresses.city":{"$regex":"^Delhi$","$options":"i"}} &f={"地址":1}

但上面的查询返回

{ “地址”:[ { "addressline1":"1 号街道", “城市”:“德里” }, { "addressline1":"4 号街道", “城市”:“钦奈” } ] }

【问题讨论】:

    标签: mongodb rest


    【解决方案1】:

    试试这个:

    https://...&f={"addresses.$":1}
    

    请注意,我在上面的查询中使用了$ projection operator

    【讨论】:

      猜你喜欢
      • 2021-02-25
      • 2018-10-08
      • 2014-02-20
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      相关资源
      最近更新 更多