Below is my collection "fullclicklogs" which has many json documents:
{
"_id" : ObjectId("58fe78dcfbe21fa7896552e8"),
"preview" : false,
"offset" : 0,
"result" : {
"tab_name" : "People-Tab",
"page_name" : "PEOPLE",
"result_number" : "1",
"page_num" : "0",
}
}
{
"_id" : ObjectId("58fe78dcfbe21fa7896552e9"),
"preview" : false,
"offset" : 0,
"result" : {
"tab_name" : "People-Tab",
"page_name" : "PEOPLE",
"result_number" : "5",
"page_num" : "0",
}
}
I would like to add a new field "DOCRANK" in my new collection "doc_rank" where DOCRANK = page_num*25+result_number
Update: Since page_num and result_number are string, I converted them into int using the following command:
>db.fullclicklogs.find().forEach( function (x) {
x.result.page_num = parseInt(x.result.page_num);
db.fullclicklogs.save(x);
});
>db.fullclicklogs.find().forEach( function (x) {
x.result.result_number = parseInt(x.result.result_number);
db.fullclicklogs.save(x);
});
Error:
2017-04-25T11:50:54.830-0700 E QUERY [thread1] TypeError: x.result is undefined :
@(shell):2:1
DBQuery.prototype.forEach@src/mongo/shell/query.js:501:1
@(shell):1:1
>
Below is the aggregation that I am using to get DOCRANK, but I am still getting value null in my result collection:
db.getCollection('fullclicklogs').aggregate( [
{
$project: {
DOCRANK: {
$let: {
vars: {
total: { $multiply: [ "$result.page_num", 25 ] },
},
in: { $add: [ "$$total", "$result.result_number" ] }
}
}
}
},
{
$out: "doc_rank1"
}
] )
Update:Used dot notation to fix my code. But now I am getting "$multiply only supports numeric types, not string"
Output of aggregation:
assert: command failed: {
"ok" : 0,
"errmsg" : "$multiply only supports numeric types, not string",
"code" : 16555,
"codeName" : "Location16555"
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
@(shell):1:1
2017-04-25T11:12:11.420-0700 E QUERY [thread1] Error: command failed: {
"ok" : 0,
"errmsg" : "$multiply only supports numeric types, not string",
"code" : 16555,
"codeName" : "Location16555"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
@(shell):1:1
May I know what's wrong with my code? Any help is appreciated. Thanks