代码之家  ›  专栏  ›  技术社区  ›  Mohammed Atif

mongodb:嵌套字符串的查询数组

  •  2
  • Mohammed Atif  · 技术社区  · 6 年前

    我有一个物品清单

    [
       { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15, type : ["hello", "world", "wassup", "yo"] } ] },
       { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
       { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 1515, type : ["hello", "wassup", "yo"] } ] },
       { item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 515, type : ["hello"] } ] },
       { item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 3515, type : ["wassup", "yo"] } ] }
    ]
    

    如何查询“type”中包含hello的所有仓库的列表?

    db.inventory.find( { "instock": { $elemMatch: {type :["hello"]}}} , {_id:0})
    

    返回只有hello的对象。

    1 回复  |  直到 6 年前
        1
  •  2
  •   mickl    6 年前

    Dot notation

    db.collection.find({ "instock.type": "hello"})
    

    MongoDB playground