Reputation: 863
I have created the following schema in MongoDB:
{
"_id" : ObjectId("52d5c71be4b0a5cd12da5e8b"),
"edges" : [
{
"edge_id" : "0",
"dst" : NumberLong(1),
"score" : ***Numeric Value***
},
{
"edge_id" : "1",
"dst" : NumberLong(6),
"score" : ***Numeric Value***
}
],
"src" : NumberLong(0)
}
The above MongoDB collection has millions of records. My requirement is:
Score
field with the random numeric value.I would like to perform the above task either using MongoDB or using a combination of MongoDB and Java.
What is the best way to perform the above task?
Upvotes: 1
Views: 1531
Reputation: 5893
With mongodb, we can update multiple documents by (for example)
db.getCollection('users').update({
username: {$eq: null}
}, {
$set: {
username: 'not_set_yet'
}
}, {
multi: true
})
If you use Typegoose
or mongoose
then you can add a pre
hook to listen the update
event, for example in typegoose
:
import {pre} from 'typegoose'
@pre<User>('save', function(next: HookNextFunction) {
// Caution! Do not use arrow function here, otherwise the `this` variable would // be point to global instead of
// this model !
this.username = this.username === 'not_set_yet' ? generateARandomUserName() : this.username
next()
})
So by combining the 2 things:
multi
to true in the update statementupdate
hookit suppose to work so that worth a try.
Upvotes: 0
Reputation: 59763
There is no way in MongoDB currently to update
a batch of documents at a single time. You can update
multiple documents with the same value only. So, you'd need to, for each document, individually set the score
field. Also, you apparently have an array, so, you'd need to update using the positional array notation the specific score
field (or set the entire array).
This will not complete in seconds. It's simply too much activity (disk and network).
If you explained more of what your trying to accomplish with this update, then there may be a better solution. Given the frequency of updates and the fact that the data is constantly changing, I'd suggest that this data structure only be kept in memory for example, and possibly served to "clients" via a web service or other transfer mechanism.
Update:
Given your requirements, I'd suggest that this is not a good fit for MongoDB (or maybe for any disk-backed database for that matter).
Upvotes: 1