我正在处理MongoDB查询
db.BH.group({
"key": {
"AccountId": true,
},
"initial": {
"count": 0
},
"reduce": function(obj, prev) {
if (true != null) if (true instanceof Array) prev.count += true.length;
else prev.count++;
},
"cond":
{"$or":[{"Url":{"$regex":"(google)"}},{"Url":{"$regex":"(facebook)"}}]}
});
Run Code Online (Sandbox Code Playgroud)
该查询在MongoDB Shell(Robomongo)中运行良好。
我已经为python编写了相同的查询。
db.BH.group({
"key": {
"AccountId": True,
},
"initial": {
"count": 0
},
"reduce": "function(obj, prev) {"
"if (true != null) if (true instanceof Array) prev.count += true.length;"
"else prev.count++;"
"}",
"cond": {"$or":[{"Url":{"$regex":"(google)"}},{"Url":{"$regex":"(facebook)"}}]}
})
Run Code Online (Sandbox Code Playgroud)
但是查询出现错误。
TypeError: group() takes at least 5 arguments (2 given)
Run Code Online (Sandbox Code Playgroud)
我尝试通过以下网站(URL)中给出的方法解决错误
http://blog.pythonisito.com/2012/05/aggregation-in-mongodb-part-1.html …
我有一个问题声明,我需要我的字段将值存储为十进制,但是在调用它时,该字段应返回一个四舍五入的Integer值
例
mymodel.myfield = 12.61
mymodel.save()
输入项
mymodel.myfield
输出量
13
如果可能的话
mymodel.myfield.get_raw
输出量
12.61
我可以使用Django自定义模型字段吗?
class MyField(models.DecimalField):
def __init__(self, verbose_name=None, name=None, max_digits=None,
decimal_places=None, **kwargs):
super(MyField, self).__init__(*args, **kwargs)
def get_raw(self, value):
return value
def to_python(self, value):
if value is None:
return value
try:
return decimal.Decimal(value)
except decimal.InvalidOperation:
raise exceptions.ValidationError(
self.error_messages['invalid'],
code='invalid',
params={'value': value},
)
Run Code Online (Sandbox Code Playgroud) django django-models python-2.7 django-custom-field django-1.6