MongoDB 查询空值



我有一个地图缩减的统计数据集,关于我们网站的流量来源。它托管在 mongohq.com,我在本地安装了 mongo 客户端,并通过本地客户端连接到 mongohq db。

如果我查询db.page_views_stats.find({"_id.offer":"chihuahua-insurance"})

我得到 21 行:

{ "_id" : { "month" : ISODate("2013-04-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 30 } }
{ "_id" : { "month" : ISODate("2013-04-01T00:00:00Z"), "source" : "none", "offer" : "chihuahua-insurance" }, "value" : { "count" : 2 } }
{ "_id" : { "month" : ISODate("2013-05-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 5 } }
{ "_id" : { "month" : ISODate("2013-05-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 495 } }
{ "_id" : { "month" : ISODate("2013-05-01T00:00:00Z"), "source" : "email", "offer" : "chihuahua-insurance" }, "value" : { "count" : 8 } }
{ "_id" : { "month" : ISODate("2013-05-01T00:00:00Z"), "source" : "none", "offer" : "chihuahua-insurance" }, "value" : { "count" : 68 } }
{ "_id" : { "month" : ISODate("2013-06-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 40 } }
{ "_id" : { "month" : ISODate("2013-06-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 63 } }
{ "_id" : { "month" : ISODate("2013-06-01T00:00:00Z"), "source" : "email", "offer" : "chihuahua-insurance" }, "value" : { "count" : 1 } }
{ "_id" : { "month" : ISODate("2013-07-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 110 } }
{ "_id" : { "month" : ISODate("2013-07-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 48 } }
{ "_id" : { "month" : ISODate("2013-08-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 219 } }
{ "_id" : { "month" : ISODate("2013-08-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 1 } }
{ "_id" : { "month" : ISODate("2013-08-01T00:00:00Z"), "source" : "tweet", "offer" : "chihuahua-insurance" }, "value" : { "count" : 1 } }
{ "_id" : { "month" : ISODate("2013-09-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 65 } }
{ "_id" : { "month" : ISODate("2013-10-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 48 } }
{ "_id" : { "month" : ISODate("2013-11-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 67 } }
{ "_id" : { "month" : ISODate("2013-11-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 1 } }
{ "_id" : { "month" : ISODate("2013-12-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 56 } }
{ "_id" : { "month" : ISODate("2013-12-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 6 } }
{ "_id" : { "month" : ISODate("2014-01-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 7 } }

它们都有一个源字段,其中一些是空的

如果我查询以下任何一项:

db.page_views_stats.find({$and:[{"_id.source": null},{"_id.offer":"chihuahua-insurance"}]})
db.page_views_stats.find({$and:[{"_id.source":{ $exists: false } },{"_id.offer":"chihuahua-insurance"}]})
db.page_views_stats.find({$and:[{"_id.source":{ $type: 10 } },{"_id.offer":"chihuahua-insurance"}]})
db.page_views_stats.find({"_id.source":{ $type: 10 },"_id.offer":"chihuahua-insurance"})
db.page_views_stats.find({"_id.source": null,"_id.offer":"chihuahua-insurance"})
db.page_views_stats.find({"_id.source":{ $exists: false },"_id.offer":"chihuahua-insurance"})

我得到 0 个结果返回。

如何找到源为空的结果?

我刚刚将您的 21 行导入到一个集合中,然后使用以下方法查询它们:

db.page_views_stats.find({"_id.source": null,"_id.offer":"chihuahua-insurance"})

这是首选查询,我得到的答案也有效。请参阅外壳会议:

> db.page_views_stats.drop();
true
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-04-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 30 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-04-01T00:00:00Z"), "source" : "none", "offer" : "chihuahua-insurance" }, "value" : { "count" : 2 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-05-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 5 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-05-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 495 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-05-01T00:00:00Z"), "source" : "email", "offer" : "chihuahua-insurance" }, "value" : { "count" : 8 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-05-01T00:00:00Z"), "source" : "none", "offer" : "chihuahua-insurance" }, "value" : { "count" : 68 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-06-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 40 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-06-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 63 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-06-01T00:00:00Z"), "source" : "email", "offer" : "chihuahua-insurance" }, "value" : { "count" : 1 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-07-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 110 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-07-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 48 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-08-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 219 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-08-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 1 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-08-01T00:00:00Z"), "source" : "tweet", "offer" : "chihuahua-insurance" }, "value" : { "count" : 1 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-09-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 65 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-10-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 48 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-11-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 67 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-11-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 1 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-12-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 56 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2013-12-01T00:00:00Z"), "source" : "cpc", "offer" : "chihuahua-insurance" }, "value" : { "count" : 6 } });
> db.page_views_stats.insert({ "_id" : { "month" : ISODate("2014-01-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 7 } });
> db.page_views_stats.find({"_id.source": null,"_id.offer":"chihuahua-insurance"});
{ "_id" : { "month" : ISODate("2013-05-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 5 } }
{ "_id" : { "month" : ISODate("2013-06-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 40 } }
{ "_id" : { "month" : ISODate("2013-07-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 110 } }
{ "_id" : { "month" : ISODate("2013-08-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 219 } }
{ "_id" : { "month" : ISODate("2013-09-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 65 } }
{ "_id" : { "month" : ISODate("2013-10-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 48 } }
{ "_id" : { "month" : ISODate("2013-11-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 67 } }
{ "_id" : { "month" : ISODate("2013-12-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 56 } }
{ "_id" : { "month" : ISODate("2014-01-01T00:00:00Z"), "source" : null, "offer" : "chihuahua-insurance" }, "value" : { "count" : 7 } }
我现在

在这个问题中找到了答案。查询需要db.page_views_stats.find({"_id.source":{ $type: 6 },"_id.offer":"chihuahua-insurance"})。我不确定为什么 6 而不是 mongodb 文档中所说的 10

最新更新