multiply in mongodb 表示只对字符串类型进行操作
multiply in mongodb saying operation only on string type
我正在尝试乘以 mongodb 中的 2 个字段。两者都是数字类型,但 mongodb 返回 $multiply only supports numeric types
。集合是:
{
"_id" : ObjectId("55e07eb54acc499bb3daae6a"),
"propertytype" : "Hotel",
"name" : "Rude Lounge2",
"costing" : {
"vegperplate" : 350,
"nonvegperplate" : 450,
"flatcharge" : 20000
},
"capacity" : {
"min" : 90,
"max" : 200
}
}
{
"_id" : ObjectId("55e07ebe4acc499bb3daae6b"),
"propertytype" : "Hotel",
"name" : "Rude Lounge3",
"costing" : {
"vegperplate" : 350,
"nonvegperplate" : 450,
"flatcharge" : 20000
},
"capacity" : {
"min" : 90,
"max" : 200
}
}
我的查询是:
> db.properties2.aggregate(
... { $project: {
... "cost": {
... $multiply:["costing.vegperplate","capacity.min"]
... }
... }
... })
错误堆栈为:
assert: command failed: {
"errmsg" : "exception: $multiply only supports numeric types, not String",
"code" : 16555,
"ok" : 0
} : aggregate failed
Error: command failed: {
"errmsg" : "exception: $multiply only supports numeric types, not String",
"code" : 16555,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:244:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1149:12)
at (shell):1:16
2015-08-29T00:13:39.173-0400 Error: command failed: {
"errmsg" : "exception: $multiply only supports numeric types, not String",
"code" : 16555,
"ok" : 0
} : aggregate failed at src/mongo/shell/assert.js:13
有人说我哪里做错了吗?
您需要在字段名称前加上 $
符号,因为您的操作是在文档中的两个不同字段上进行的。
db.properties2.aggregate([
{
"$project": {
"cost": { "$multiply": [ "$costing.vegperplate", "$capacity.min" ]}
}
])
我正在尝试乘以 mongodb 中的 2 个字段。两者都是数字类型,但 mongodb 返回 $multiply only supports numeric types
。集合是:
{
"_id" : ObjectId("55e07eb54acc499bb3daae6a"),
"propertytype" : "Hotel",
"name" : "Rude Lounge2",
"costing" : {
"vegperplate" : 350,
"nonvegperplate" : 450,
"flatcharge" : 20000
},
"capacity" : {
"min" : 90,
"max" : 200
}
}
{
"_id" : ObjectId("55e07ebe4acc499bb3daae6b"),
"propertytype" : "Hotel",
"name" : "Rude Lounge3",
"costing" : {
"vegperplate" : 350,
"nonvegperplate" : 450,
"flatcharge" : 20000
},
"capacity" : {
"min" : 90,
"max" : 200
}
}
我的查询是:
> db.properties2.aggregate(
... { $project: {
... "cost": {
... $multiply:["costing.vegperplate","capacity.min"]
... }
... }
... })
错误堆栈为:
assert: command failed: {
"errmsg" : "exception: $multiply only supports numeric types, not String",
"code" : 16555,
"ok" : 0
} : aggregate failed
Error: command failed: {
"errmsg" : "exception: $multiply only supports numeric types, not String",
"code" : 16555,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:244:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1149:12)
at (shell):1:16
2015-08-29T00:13:39.173-0400 Error: command failed: {
"errmsg" : "exception: $multiply only supports numeric types, not String",
"code" : 16555,
"ok" : 0
} : aggregate failed at src/mongo/shell/assert.js:13
有人说我哪里做错了吗?
您需要在字段名称前加上 $
符号,因为您的操作是在文档中的两个不同字段上进行的。
db.properties2.aggregate([
{
"$project": {
"cost": { "$multiply": [ "$costing.vegperplate", "$capacity.min" ]}
}
])