I have a collection with docs like:
{ID: xxx, firstCreateDate: xxx, isActive: false}
- I want to find all the docs where
isActive = false, then updateisActiveto true, and also updatefirstCreateDateto current date if not set before. I'm not sure how to conditionally update filed firstCreateDate. Couldn't get the conditionally update working right now. GotfirstCreateDate = {$cond:[{"firstCreateDate":{"$exists":false}}, "$firstCreateDate", newdata]} - I also want to use a custom function to generate a new field internalID based on original
IDfield. I tried to useUpdateMany(), but seems like there is no way to get the value and calculate the new value using your own function. I'm thinking about useFind()to find all docs, then decode it, and then update docs one by one based on id. Is there any better solution for this?
This is what I have now:
filter := bson.M{"isActive": false}
update := bson.M{"set": bson.M{"isActive": true, "firstCreateDate": bson.M{
"$cond": bson.A{
bson.M{"firstCreateDate", bson.M{"$exists", false}},
"$firstCreateDate",
newDate,
}
}}}
collection.UpdateByID(
context.Background(),
id,
update,
)
I'm using mongo-driver lib, anyone can help