如何编写一个返回具有特定键的记录的Mongo查询?

时间:2016-03-28 02:36:25

标签: mongodb

我在MongoDB中填写了一些记录

db.products.insert({ "_id" : { "oid" : "507d95d5719dbef170f15bfe" }, "name" : "Phone Service Basic Plan", "type" : "service", "monthly_price" : 40, "limits" : { "voice" : { "units" : "minutes", "n" : 400, "over_rate" : 0.05 }, "data" : { "units" : "gigabytes", "n" : 20, "over_rate" : 1 }, "sms" : { "units" : "texts sent", "n" : 100, "over_rate" : 0.001 } }, "term_years" : 2 })

db.products.insert({ "_id" : { "oid" : "507d95d5719dbef170f15bff" }, "name" : "Phone Service Core Plan", "type" : "service", "monthly_price" : 60, "limits" : { "voice" : { "units" : "minutes", "n" : 1000, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "term_years" : 1 })

db.products.insert({ "_id" : { "oid" : "507d95d5719dbef170f15c00" }, "name" : "Phone Service Family Plan", "type" : "service", "monthly_price" : 90, "limits" : { "voice" : { "units" : "minutes", "n" : 1200, "over_rate" : 0.05 }, "data" : { "n" : "unlimited", "over_rate" : 0 }, "sms" : { "n" : "unlimited", "over_rate" : 0 } }, "sales_tax" : true, "term_years" : 2 })

db.products.insert({ "_id" : { "oid" : "507d95d5719dbef170f15c01" }, "name" : "Cable TV Basic Service Package", "type" : "tv", "monthly_price" : 50, "term_years" : 2, "cancel_penalty" : 25, "sales_tax" : true, "additional_tarriffs" : [ { "kind" : "federal tarriff", "amount" : { "percent_of_service" : 0.06 } }, { "kind" : "misc tarriff", "amount" : 2.25 } ] })

现在,如果我需要找到,我执行的电视有多少种产品类型

db.products.find( { type : "tv" } ).count()

返回1(正确)。

同样,如果我需要找到有多少产品有语音限制,查询会是什么?

我尝试使用db.products.find( { limits : "voice" } ).count()但它返回0,而它应该是3.

1 个答案:

答案 0 :(得分:1)

您需要使用http://php.net/manual/en/function.move-uploaded-file.php子句

db.products.find({ "limits.voice" : { $exists : true, $ne : null } })