MongoDB查询null

时间:2014-01-07 10:06:25

标签: mongodb

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

如果我查询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结果。

如何找到source为null的结果?

2 个答案:

答案 0 :(得分:2)

我刚刚将21行导入了一个集合,然后我用它们查询:

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

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

> 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 } }

答案 1 :(得分:0)

我现在在this question找到了答案。查询需要是db.page_views_stats.find({“_ id.source”:{$ type:6},“_ id.offer”:“chihuahua-insurance”})。我不确定mongodb docs

中所说的为6而不是10