【问题标题】:MongoDB: How to fetch value for particular sub-field and its subsequent field in the array? [duplicate]MongoDB:如何获取数组中特定子字段及其后续字段的值? [复制]
【发布时间】:2015-10-31 11:53:42
【问题描述】:

我是 MongoDB 的新手。我正在尝试从文档中的数组中获取特定 sku 的字段值。我试过查询 ({"items.sku":"abc"},{"items.price":1}) 但它给出了两个 sku 的价格。如果有人能告诉我如何获得仅 sku:"abc" 即 3.5 的价格,我将不胜感激?

{  "_id": 123,
     "store": "store1",
     "items": [ { sku: "abc", qty: 4, price: 3.5 },
              { sku: "def", qty: 3, price: 2.5 } ]
}

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    请尝试:

    db.getCollection('abhey').find({"items.sku":"abc"},{_id: 0, 'items.price.$': 1})
    

    如果您需要更多帮助,请随时写信。

    【讨论】:

    • 谢谢阿比。我试过你的答案,它正在工作。
    • 不错!你能投票给我的答案吗?
    • 我需要更多积分(因为我是新会员)才能投票。感谢您的帮助,下次一定会投票。
    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 2019-08-12
    • 2022-12-16
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多