MongoDB 嵌套字段的乘法
Multiplication on nested fields on MongoDB
在 MongoDB 的数据库中,我试图按日期对一些数据进行分组(一年中的每一天一组),然后添加一个附加字段,该字段将是乘积的结果两个已经存在的字段。
数据结构为:
{
"_id" : ObjectId("567a7c6d9da4bc18967a3947"),
"units" : 3.0,
"price" : 50.0,
"name" : "Name goes here",
"datetime" : ISODate("2015-12-23T10:50:21.560+0000")
}
我首先尝试了使用 $project
的两阶段方法,然后 $group
像这样
db.things.aggregate(
[
{
$project: {
"_id" : 1,
"name" : 1,
"units" : 1,
"price" : 1,
"datetime":1,
"unitsprice" : { $multiply: [ "$price", "$units" ] }
}
},
{
$group: {
"_id" : {
"day" : {
"$dayOfMonth" : "$datetime"
},
"month" : {
"$month" : "$datetime"
},
"year" : {
"$year" : "$datetime"
}
},
"things" : {
"$push" : "$$ROOT"
}
}
}
],
)
在这种情况下,第一步($project
)给出了预期的输出(具有 unitsprice 的预期值),但是在执行第二步 $group
时,它会输出此错误:
"errmsg":$multiply only supports numeric types, not String",
"code":16555
我也试着扭转局面,先做 $group
步骤,然后 $project
db.things.aggregate(
[
{
$group: {
"_id" : {
"day" : {
"$dayOfMonth" : "$datetime"
},
"month" : {
"$month" : "$datetime"
},
"year" : {
"$year" : "$datetime"
}
},
"things" : {
"$push" : "$$ROOT"
}
}
},
{
$project: {
"_id" : 1,
"things":{
"name" : 1,
"units" : 1,
"price" : 1,
"datetime":1,
"unitsprice" : { $multiply: [ "$price", "$units" ] }
}
}
}
],
);
但是在这种情况下,乘法的结果是:unitsprice:null
有没有办法做这个乘法?此外,最好以输出不会包含嵌套字段的方式执行此操作,因此它看起来像:
{"_id":
"units":
"price":
"name":
"datetime":
"unitsprice":
}
提前致谢
PS:我是运行MongoDB3.2
终于找到错误了。导入其中一个字段时,一些 price
字段被创建为 string
。意外的是,在project
这一步第一次做multiplication
的时候,错误并没有出来(一直输出正常,直到遇到第一个错误的字段,然后就停止了),但是当做group
步骤。
为了找到我使用此查询的文本字段:
db.things.find( { price: { $type: 2 } } );
感谢提示
在 MongoDB 的数据库中,我试图按日期对一些数据进行分组(一年中的每一天一组),然后添加一个附加字段,该字段将是乘积的结果两个已经存在的字段。
数据结构为:
{
"_id" : ObjectId("567a7c6d9da4bc18967a3947"),
"units" : 3.0,
"price" : 50.0,
"name" : "Name goes here",
"datetime" : ISODate("2015-12-23T10:50:21.560+0000")
}
我首先尝试了使用 $project
的两阶段方法,然后 $group
像这样
db.things.aggregate(
[
{
$project: {
"_id" : 1,
"name" : 1,
"units" : 1,
"price" : 1,
"datetime":1,
"unitsprice" : { $multiply: [ "$price", "$units" ] }
}
},
{
$group: {
"_id" : {
"day" : {
"$dayOfMonth" : "$datetime"
},
"month" : {
"$month" : "$datetime"
},
"year" : {
"$year" : "$datetime"
}
},
"things" : {
"$push" : "$$ROOT"
}
}
}
],
)
在这种情况下,第一步($project
)给出了预期的输出(具有 unitsprice 的预期值),但是在执行第二步 $group
时,它会输出此错误:
"errmsg":$multiply only supports numeric types, not String", "code":16555
我也试着扭转局面,先做 $group
步骤,然后 $project
db.things.aggregate(
[
{
$group: {
"_id" : {
"day" : {
"$dayOfMonth" : "$datetime"
},
"month" : {
"$month" : "$datetime"
},
"year" : {
"$year" : "$datetime"
}
},
"things" : {
"$push" : "$$ROOT"
}
}
},
{
$project: {
"_id" : 1,
"things":{
"name" : 1,
"units" : 1,
"price" : 1,
"datetime":1,
"unitsprice" : { $multiply: [ "$price", "$units" ] }
}
}
}
],
);
但是在这种情况下,乘法的结果是:unitsprice:null
有没有办法做这个乘法?此外,最好以输出不会包含嵌套字段的方式执行此操作,因此它看起来像:
{"_id":
"units":
"price":
"name":
"datetime":
"unitsprice":
}
提前致谢
PS:我是运行MongoDB3.2
终于找到错误了。导入其中一个字段时,一些 price
字段被创建为 string
。意外的是,在project
这一步第一次做multiplication
的时候,错误并没有出来(一直输出正常,直到遇到第一个错误的字段,然后就停止了),但是当做group
步骤。
为了找到我使用此查询的文本字段:
db.things.find( { price: { $type: 2 } } );
感谢提示