0

我有如下的monogodb数据结构:

        {
       "_id" : ObjectId("512c4c468c08631ff0dbb02c"),
       "ReferenceNumber" : "MongoDb",
       "Title" : "Shashi",
       "CreatedUserId" : "users/1027",
       "Content" : "s",
       "CustomerId" : "customers/257",
       "StatusDefinitionId" : "sd",
        "ItemMetadata" : [{
       "MetadataId" : "MetadataDefinitions/839",
       "MetadataName" : "DropDown",
       "MetadataValue" : "78"
        }, {
      "MetadataId" : "MetadataDefinitions/839",
      "MetadataName" : "DropDown",
      "MetadataValue" : "DropDown3"
       }, {
       "MetadataId" : "MetadataDefinitions/838",
       "MetadataName" : "Number1",
       "MetadataValue" : "546"
        }],
        "Portfolios" : [{
        "_id" : "portfolios/226",
         "IsDefault" : true
         }]
        }

第二个项目集合:-

                        {
                      "_id" : ObjectId("512c4c468c08631ff0dbb02c"),
                      "ReferenceNumber" : "MongoDb",
                       "Title" : "Shashi",
                       "CreatedUserId" : "users/1027",
                       "Content" : "s",
                       "CustomerId" : "customers/257",
                       "StatusDefinitionId" : "sd",
                        "ItemMetadata" : [{
                     "MetadataId" : "MetadataDefinitions/839",
                   "MetadataName" : "DropDown",
                     "MetadataValue" : "78"
                     }, {
                   "MetadataId" : "MetadataDefinitions/839",
                    "MetadataName" : "DropDown",
                     "MetadataValue" : "DropDown4"
                       }, {
                     "MetadataId" : "MetadataDefinitions/838",
                     "MetadataName" : "Number1",
                    "MetadataValue" : "546"
                     }],
                     "Portfolios" : [{
                    "_id" : "portfolios/226",
                       "IsDefault" : true
                       }]
                       }

我想在 mongodb c# 中获取查询。我只需要返回第一个项目集合。

(ItemMetadata.MetadataName= "DropDown" and ItemMetadata.MetadataValue="78" and ItemMetadata.MetadataName= "DropDown" and ItemMetadata.MetadataValue="DropDown3") 

与 ItemMetadata 的第二个孩子类似。有点像这样

    Query.EQ("ItemMetadata.MetadataName", "DropDown") and (Query.EQ("ItemMetadata.MetadataValue", "78"]
4

1 回答 1

0

In terms of which document(s) to return, you query is then "how do I specify that only documents containing an array element where both MetadataName and MetadataValue match in the same element" - the $elemMatch is the operator to use.

In terms of what parts of the document to return, your only choice is to return the whole filed or not to return a whole field. In regular queries, you can't a subset of the values in the stored array. (Although you could use the Aggregation framework $unwind, but not really necessary or recommended high transaction reads)

于 2013-03-05T18:53:01.733 回答