eliminate same data while inserting multiple records

Hi i a have huge rows like 50m+ some of them are duplicate values

i don't want mongo driver add them db if there is exactly same key & value exists

an example data

[{
    num: "+905081472117",
    name: "sulocan"
},{
    num: "+905351224578",
    name: "ahmet"
},{
    num: "+905251223578",
    name: "ahmet"
},{
    num: "+905081472117",
    name: "differ"
},{
    num: "+905081472117",
    name: "sulocan"
}]

i don't want it only just checks num i want it can add same num with diff name but not add same num with same name

as the example data i give i only want it doesn't add last value of array

exports.addnumber = function(num, cb) {
    db.collection('numbers', function(err, collection) {
        collection.insert(num, {safe:true}, function(err, result) {
            if (err) {
                res({'error':'An error has occurred'});
            } else {
                //console.log('Success: ' + JSON.stringify(result));
                res(result);
            }
        });
    });
}

that is the code i use for inserting values to db

i am newbie , just began to use mongodb

best regards

Upvotes: 1

Views: 41

Answers (1)

Zava
Zava

Reputation: 400

You can add an index on key and value with the unique and dropDups properties. This would help.

Upvotes: 1

Related Questions