简体   繁体   English

mongodb查询以在json中查找值

[英]mongodb query to find value in json

I started working with MongoDB. 我开始使用MongoDB。 I prepared some basic training JSON: 我准备了一些基本的JSON训练:

{
  "header": {
      "Hotel": {
      "data": [
          {
          "name": "Hilton",
          "Id": "1231213421"
          }
      ]
      },
      "standard": "5",
      "priceStage": "4"
  },
  "data": {
      "http": {
      "strean": {}
      }
  }
}

and I wrote a query like this: 我写了这样的查询:

db.hotel.find( "data": { Id: "1231213421"})

Why query does not return anything? 为什么查询不返回任何内容?

You're trying to match on an element within an array and you don't need to match on the entire array. 您正在尝试匹配数组中的元素,而无需匹配整个数组。 So,something like the following will work: 因此,类似以下的内容将起作用:

db.hotel.find({"header.Hotel.data": {"$elemMatch": {"Id": "1231213421"}}} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM