db.auto_complete.find()
{ "_id" : ObjectId("6239a3c93a8e84c1b46d1bf4"), "category" : "음식", "keyword" : "삼겹살", "weight" : 0, "shard" : 5, "searchCount" : 3, "satisfactionCount" : 4, "force" : false }
but i want to update weight = (searchCount X 0.5 satisfactionCount X 0.5)
my wanted result =
{ "_id" : ObjectId("6239a3c93a8e84c1b46d1bf4"), "category" : "음식", "keyword" : "삼겹살", "weight" : 3, "shard" : 5, "searchCount" : 3, "satisfactionCount" : 4, "force" : false }
weight = ( 3(searchCount)X0.5 4(satisfactionCount)X0.5 ) =>3.5 ( round off) => 3
i have tried with $set
but i didn't solved this problem how to query this problem with mongo
and if weight have decimal point i want round off the decimal point
please help me....
CodePudding user response:
Maybe something like this:
db.collection.update({},
[
{
$addFields: {
// weight = (searchCount X 0.5 satisfactionCount X 0.5)
weight: {
$trunc: [
{
"$sum": [
{
"$multiply": [
"$searchCount",
0.5
]
},
{
"$multiply": [
"$satisfactionCount",
0.5
]
}
]
},
0
]
}
}
}
],{multi:true})
Explained:
Use the the update with aggregation pipeline method ( mongodb 4.2 ) to replace the weight value with the rounded calculated based on provided formula. Add the option { multi:true } to apply to all documents in the collection.