我有一份文件......
doc =
{
"Loan_Num" : "Loan002",
"Info" : "Loan for processing details."
"Loan_Items" : [
{
"Invoice_Num" : "32134324",
"Quantity" : 1,
"Amount_Required" : "£4"
},
{
"Book_Barcode" : "22222",
"Quantity" : 1,
"Amount_Required" : "£2"
"Amount_Payed" : true
}
]
};
我试图创建一个查询以带回任何包含" Amount_Required"关键,但没有" Amount_Payed"键。
所以从上面的文件中我会期待这个结果......
{
"Loan_Num" : "Loan002",
"Info" : "Loan for processing details."
"Loan_Items" : [
{
"Invoice_Num" : "32134324",
"Quantity" : 1,
"Amount_Required" : "£4"
}
]
};
这是我的Java代码......
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.append("Loan_Num", "Loan002");
searchQuery.append("Loan_Items.Fine_Amount", new BasicDBObject("$exists", true));
searchQuery.append("Loan_Items.Fine_Payed", new BasicDBObject("$exists", false));
问题是它不会返回任何东西,因为" Amount_Played"存在于数组的第二个元素中。我怎么能实现这个结果? - 非常感谢
答案 0 :(得分:0)
您可以尝试使用$elemMatch:
您的查询将如下所示:
db.collection.find({"Loan_Items" : {$elemMatch : { "Amount_Required" : { $exists : true}, "Amount_Payed" : { $exists : false}}}})
希望这有帮助!