如何在猫鼬中查找数据?

How to find data in mongoose?

我试过这个命令,但它没有输出。 我想找到 formatted_address 与菲律宾北拉瑙岛伊利甘市

Postjob.find({'location.results[2].formatted_address': 'Iligan City, Lanao del Norte, Philippines'})

{
  "user": ObjectId("5557411628f66792558f60d3"),
  "created": ISODate("2015-05-16T15:06:15.478Z"),
  "map": {
    "zoom": "3",
    "position": "8.22992420109622,124.24962043762207"
  },
  "location": {
    "status": "OK",
    "results": [
      {
        "types": [
          "route"
        ],
        "place_id": "ChIJhbJ4M3N2VTIRR34qr5Bf3OA",
        "geometry": {
          "viewport": {
            "southwest": {
              "lng": 124.24813966971,
              "lat": 8.2281481697085
            },
            "northeast": {
              "lng": 124.25083763029,
              "lat": 8.2308461302915
            }
          },
          "location_type": "APPROXIMATE",
          "location": {
            "lng": 124.2494887,
            "lat": 8.2294971
          },
          "bounds": {
            "southwest": {
              "lng": 124.2494355,
              "lat": 8.229059
            },
            "northeast": {
              "lng": 124.2495418,
              "lat": 8.2299353
            }
          }
        },
        "formatted_address": "Ubaldo Laya Avenue, Iligan City, 9200 Lanao del Norte, Philippines",
        "address_components": [
          {
            "types": [
              "route"
            ],
            "short_name": "Ubaldo Laya Ave",
            "long_name": "Ubaldo Laya Avenue"
          },
          {
            "types": [
              "neighborhood",
              "political"
            ],
            "short_name": "Pala-o",
            "long_name": "Pala-o"
          },
          {
            "types": [
              "locality",
              "political"
            ],
            "short_name": "Iligan City",
            "long_name": "Iligan City"
          },
          {
            "types": [
              "administrative_area_level_2",
              "political"
            ],
            "short_name": "Lanao del Norte",
            "long_name": "Lanao del Norte"
          },
          {
            "types": [
              "administrative_area_level_1",
              "political"
            ],
            "short_name": "Northern Mindanao",
            "long_name": "Northern Mindanao"
          },
          {
            "types": [
              "country",
              "political"
            ],
            "short_name": "PH",
            "long_name": "Philippines"
          },
          {
            "types": [
              "postal_code"
            ],
            "short_name": "9200",
            "long_name": "9200"
          }
        ]
      },
      {
        "types": [
          "neighborhood",
          "political"
        ],
        "place_id": "ChIJTc3qwgx2VTIRu-0j_nFpfMg",
        "geometry": {
          "viewport": {
            "southwest": {
              "lng": 124.2402704,
              "lat": 8.2213317
            },
            "northeast": {
              "lng": 124.2650056,
              "lat": 8.2396977
            }
          },
          "location_type": "APPROXIMATE",
          "location": {
            "lng": 124.2560525,
            "lat": 8.2262687
          },
          "bounds": {
            "southwest": {
              "lng": 124.2402704,
              "lat": 8.2213317
            },
            "northeast": {
              "lng": 124.2650056,
              "lat": 8.2396977
            }
          }
        },
        "formatted_address": "Pala-o, Iligan City, Lanao del Norte, Philippines",
        "address_components": [
          {
            "types": [
              "neighborhood",
              "political"
            ],
            "short_name": "Pala-o",
            "long_name": "Pala-o"
          },
          {
            "types": [
              "locality",
              "political"
            ],
            "short_name": "Iligan City",
            "long_name": "Iligan City"
          },
          {
            "types": [
              "administrative_area_level_2",
              "political"
            ],
            "short_name": "Lanao del Norte",
            "long_name": "Lanao del Norte"
          },
          {
            "types": [
              "administrative_area_level_1",
              "political"
            ],
            "short_name": "Northern Mindanao",
            "long_name": "Northern Mindanao"
          },
          {
            "types": [
              "country",
              "political"
            ],
            "short_name": "PH",
            "long_name": "Philippines"
          }
        ]
      },
      {
        "types": [
          "locality",
          "political"
        ],
        "place_id": "ChIJDVTJKLN5VTIRO9DwoqsI4uY",
        "geometry": {
          "viewport": {
            "southwest": {
              "lng": 124.1653929,
              "lat": 8.002117
            },
            "northeast": {
              "lng": 124.5800686,
              "lat": 8.3233239
            }
          },
          "location_type": "APPROXIMATE",
          "location": {
            "lng": 124.2452422,
            "lat": 8.2280209
          },
          "bounds": {
            "southwest": {
              "lng": 124.1653929,
              "lat": 8.002117
            },
            "northeast": {
              "lng": 124.5800686,
              "lat": 8.3233239
            }
          }
        },
        "formatted_address": "Iligan City, Lanao del Norte, Philippines",
        "address_components": [
          {
            "types": [
              "locality",
              "political"
            ],
            "short_name": "Iligan City",
            "long_name": "Iligan City"
          },
          {
            "types": [
              "administrative_area_level_2",
              "political"
            ],
            "short_name": "Lanao del Norte",
            "long_name": "Lanao del Norte"
          },
          {
            "types": [
              "administrative_area_level_1",
              "political"
            ],
            "short_name": "Northern Mindanao",
            "long_name": "Northern Mindanao"
          },
          {
            "types": [
              "country",
              "political"
            ],
            "short_name": "PH",
            "long_name": "Philippines"
          }
        ]
      },
      {
        "types": [
          "administrative_area_level_2",
          "political"
        ],
        "place_id": "ChIJBXUpcVy5VTIRh-s9Mn1Hh58",
        "geometry": {
          "viewport": {
            "southwest": {
              "lng": 123.621328,
              "lat": 7.6934621
            },
            "northeast": {
              "lng": 124.573305,
              "lat": 8.3414729
            }
          },
          "location_type": "APPROXIMATE",
          "location": {
            "lng": 123.8857747,
            "lat": 7.8721811
          },
          "bounds": {
            "southwest": {
              "lng": 123.621328,
              "lat": 7.6934621
            },
            "northeast": {
              "lng": 124.573305,
              "lat": 8.3414729
            }
          }
        },
        "formatted_address": "Lanao del Norte, Philippines",
        "address_components": [
          {
            "types": [
              "administrative_area_level_2",
              "political"
            ],
            "short_name": "Lanao del Norte",
            "long_name": "Lanao del Norte"
          },
          {
            "types": [
              "administrative_area_level_1",
              "political"
            ],
            "short_name": "Northern Mindanao",
            "long_name": "Northern Mindanao"
          },
          {
            "types": [
              "country",
              "political"
            ],
            "short_name": "PH",
            "long_name": "Philippines"
          }
        ]
      },
      {
        "types": [
          "administrative_area_level_1",
          "political"
        ],
        "place_id": "ChIJjWvt7eRkADMR5MfVbPLs2mM",
        "geometry": {
          "viewport": {
            "southwest": {
              "lng": 123.5556265,
              "lat": 7.3991609
            },
            "northeast": {
              "lng": 125.444464,
              "lat": 9.2580606
            }
          },
          "location_type": "APPROXIMATE",
          "location": {
            "lng": 124.6856509,
            "lat": 8.0201635
          },
          "bounds": {
            "southwest": {
              "lng": 123.5556265,
              "lat": 7.3991609
            },
            "northeast": {
              "lng": 125.444464,
              "lat": 9.2580606
            }
          }
        },
        "formatted_address": "Northern Mindanao, Philippines",
        "address_components": [
          {
            "types": [
              "administrative_area_level_1",
              "political"
            ],
            "short_name": "Northern Mindanao",
            "long_name": "Northern Mindanao"
          },
          {
            "types": [
              "country",
              "political"
            ],
            "short_name": "PH",
            "long_name": "Philippines"
          }
        ]
      },
      {
        "types": [
          "country",
          "political"
        ],
        "place_id": "ChIJY96HXyFTQDIRV9opeu-QR3g",
        "geometry": {
          "viewport": {
            "southwest": {
              "lng": 114.583481,
              "lat": 3.6346296
            },
            "northeast": {
              "lng": 130.6600275,
              "lat": 19.7610407
            }
          },
          "location_type": "APPROXIMATE",
          "location": {
            "lng": 121.774017,
            "lat": 12.879721
          },
          "bounds": {
            "southwest": {
              "lng": 116.7029193,
              "lat": 4.5870339
            },
            "northeast": {
              "lng": 126.6043837,
              "lat": 19.5740241
            }
          }
        },
        "formatted_address": "Philippines",
        "address_components": [
          {
            "types": [
              "country",
              "political"
            ],
            "short_name": "PH",
            "long_name": "Philippines"
          }
        ]
      }
    ]
  },
  "jobdescription": "test",
  "name": "test",
  "__v": NumberInt(0)
}

我认为你的查询是错误的。您可以使用 $elemMatch(projection) [1] 来解决这个问题。

示例:

Postjob.find({"user": ObjectId("5557411628f66792558f60d3")}, {"location.results": {$elemMatch: {formatted_address: "Iligan City, Lanao del Norte, Philippines" } } });

希望对您有所帮助。

[1]。 http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/