2016-09-17 89 views
0

我需要找到具有product.typeCode中的元素[ 「800」, 「200」, 「400」]

{ 
    "_id": "[email protected]", 
    "_class": "com.mongodb.BasicDBObject", 
    "accounts": [{ 
     "number": "96398-00910620286__DISABILITY", 
     "product": { 
      "typeCode": "400", 
      "nameCode": "401" 
     }, 
     "dependents": [], 
     "_id": "[email protected]__DISABILITYDSB" 
    }, { 
     "number": "96398-00910620286__LIFECNV", 
     "product": { 
      "typeCode": "300", 
      "nameCode": "LIFECNV" 
     }, 
     "dependents": [], 
     "_id": "[email protected]" 
    }] 
} 

我寫此查詢,但它沒有返回結果

find({ 
    accounts: { 
     $elemMatch: { 
      product: { 
       typeCode: { 
        $in: ["400"] 
       } 
      } 
     } 
    } 
}) 

回答

1

請嘗試以下查詢。您可以在IN子句中添加附加值(800,200,400)。

db.collection.find({ "accounts.product.typeCode" : { 
        $in: ["400"] 
       } 
      } 
    ); 

與所有三個值: -

db.productlist.find({ "accounts.product.typeCode" : { 
        $in: ["400", "800", "200"] 
       } 
      } 
    );