还有除点符号之外的其他方式吗?

时间:2011-11-03 13:13:13

标签: php mongodb

我有以下文件:

{
   "_id":ObjectId("4eb28bd60078621a7d0000aa"),
   "metadata":{
      "xuuid":{
         "scope":"g",
         "version":1,
         "uuid":"0c1969c55b0241bfa3d37c6ceb900bba"
      },
      "custom":[

      ],
      "file":{
         "staticDataProviderId":4,
         "contentType":"factsheet",
         "name":"hugos tagebuch",
         "size":2345,
         "source":"bamldocuments",
         "mimeType":"application/pdf",
         "created":1319623020,
         "modified":1319623020,
         "accessed":1349623020,
         "locales":[
            "EN_us",
            "CH_fr"
         ],
         "countries":[
            "CH"
         ],
         "status":"published"
      },
      "links":[
         {
            "type":"instrument",
            "key":5
         },
         {
            "type":"instrument",
            "key":3
         }
      ]
   },
   "chunkSize":262144,
   "length":14,
   "md5":"2dd900d9be40e2dd3fc69cc77c14726c"
}

我可以通过以下查询找到它:

db.fs.files.find({"metadata.links":{"type":"instrument","key":5}})

但是我想要那样的somtehing(与我的文档不匹配,元数据中其他键的原因):

db.fs.files.find({"metadata"{"links":{"type":"instrument","key":5}}})

作为背景,在我的PHP代码中,我希望有一个“查找”功能,它只能在元数据中应用标准,所以我需要一些不那么好的东西:

public function find(array $criteria)
{
   $extendedCriteria = array();
   foreach($criteria as $key => $value) {
      $extendedCriteria['metadata.'.$key => $value]; 
   }
   ...
}

1 个答案:

答案 0 :(得分:0)

the page on dot notation的末尾,他们提到了$ elemMatch运算符,可以这样使用:

db.foo.find({foo: {"$elemMatch": {shape: "square", color: "purple"}}})

...匹配“foo”元素,其字段形状的值为“square”,字段颜色的值为“purple”。

我不是百分百肯定这会做你想要的,但要检查出来。