代码之家  ›  专栏  ›  技术社区  ›  Umair Ayub

如何根据mongo中的id和另一个字段选择文档?

  •  0
  • Umair Ayub  · 技术社区  · 6 年前

    我对Mongo很陌生,我有收藏

    { 
        "_id" : "297637", 
        "polygon" : {
            "1" : {
                "direction5" : {
                    "lat" : 40.73279100000004, 
                    "lon" : -74.027161, 
                    "delivery_fee" : NumberInt(300), 
                    "delivery_estimate" : NumberInt(45), 
                    "delivery_offered_to_diner_location" : false
                }
            }
        }
    }
    

    我希望query选择所有id=297637和polygon=1的文档 平均选择多边形内“direction1”、“direction2”、“direction3”等所有行=1

    我试过了

    { $and: [ { "_id": "297637" }, { "polygon": "1" } ] }
    

    但没有任何结果。

    也许是因为 polygon 1 Object 或者什么?

    enter image description here

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

    你不能用 { "polygon": "1" } 因为这个语句检查 polygon 字段等于 "1" 在文档中,它是一个带有一个字段的嵌套对象 “1” .

    在MongoDB中全部 _id 字段是唯一的,这意味着 db.col.find({_id: "297637"}) 只给你一份文件。但是如果你还想检查 多边形 得到零个或一个结果,你可以检查路径 "polygon.1" 存在于您的文档中,使用 $exists 接线员。

    db.col.find({_id: "297637", "polygon.1": { $exists: true } })
    

    或者你可以检查 polygon.1 是一个使用 $type 操作员:

    db.col.find({_id: "297637", "polygon.1": { $type: "object" } });