Mongodb,osm 街道地图,独立用户
Mongodb, osm street maps, unique users
我将一个 json 文件导入到我的数据库中,数据库本身可以正常工作。
如果使用
print db.points.find_one()
我的输出看起来像:
{u'id': u'342902', u'_id': ObjectId('555af76a029d3b1b0ff9a4be'), u'type': u'node', u'pos': [48.9979746, 8.3719741], u'created': {u'changeset': u'7105928', u'version': u'4', u'uid': u'163673', u'timestamp': u'2011-01-27T18:05:54Z', u'user': u'Free_Jan'}}
这正是我想要的。但是如果我想获得不同用户的数量:
print db.points.distinct({"created.user"}).length
我收到错误:
TypeError: key must be an instance of basestring
这表明我要搜索的内容不存在(如上图所示)。我在这里错过了什么?
您的 distinct 子句中存在语法错误 - 它应该是 .distinct("created.user")
而不是 .distinct({"created.user"})
。
见the MongoDB documentation for distinct。
编辑:到return的长度,因此,类似于:
print len(db.points.distinct("created.user"));
... 或:
print db.points.distinct("created.user").__len__();
有关获取 Python 中列表大小的信息,请参阅 this answer。
警告:我不是 Python 专家,所以长度位可能并不完美...
我遇到了同样的问题。
print len(db.points.distinct("created.user"))
有效。
但是,
print db.points.distinct("created.user").__len__();
才不是。
错误原因是大括号:
db.points.distinct({"created.user"})
应该阅读
db.points.distinct("created.user")
此外,'.length' 是一种 mongo bash 方法。在 pymongo 中,你使用 'len()'。即:
db.points.distinct({"created.user"}).length
应该阅读
print len(db.points.distinct("created.user"))
除非你使用 mongo bash/shell.
我将一个 json 文件导入到我的数据库中,数据库本身可以正常工作。
如果使用
print db.points.find_one()
我的输出看起来像:
{u'id': u'342902', u'_id': ObjectId('555af76a029d3b1b0ff9a4be'), u'type': u'node', u'pos': [48.9979746, 8.3719741], u'created': {u'changeset': u'7105928', u'version': u'4', u'uid': u'163673', u'timestamp': u'2011-01-27T18:05:54Z', u'user': u'Free_Jan'}}
这正是我想要的。但是如果我想获得不同用户的数量:
print db.points.distinct({"created.user"}).length
我收到错误:
TypeError: key must be an instance of basestring
这表明我要搜索的内容不存在(如上图所示)。我在这里错过了什么?
您的 distinct 子句中存在语法错误 - 它应该是 .distinct("created.user")
而不是 .distinct({"created.user"})
。
见the MongoDB documentation for distinct。
编辑:到return的长度,因此,类似于:
print len(db.points.distinct("created.user"));
... 或:
print db.points.distinct("created.user").__len__();
有关获取 Python 中列表大小的信息,请参阅 this answer。
警告:我不是 Python 专家,所以长度位可能并不完美...
我遇到了同样的问题。
print len(db.points.distinct("created.user"))
有效。
但是,
print db.points.distinct("created.user").__len__();
才不是。
错误原因是大括号:
db.points.distinct({"created.user"})
应该阅读
db.points.distinct("created.user")
此外,'.length' 是一种 mongo bash 方法。在 pymongo 中,你使用 'len()'。即:
db.points.distinct({"created.user"}).length
应该阅读
print len(db.points.distinct("created.user"))
除非你使用 mongo bash/shell.