Andrejovic Andrej
Andrejovic Andrej

Reputation: 218

Compare values in two Collections

I'm using a Mongo DB. Have a two collections main_hikanshou and main_kokyaku. main_hikanshou contains only one column - phone_number. main_kokyaku contains about 10 columns where one is same phone_number. This two collections have about 150000 values, I would like to compare this two collections and get .csv output on matched values in computational-light way...

I'm using this js to just to print it, but it takes too long for using it this way...

var list = []
db.main_hikanshou.find().forEach(function(doc1){
        var doc2 = db.main_kokyaku.findOne({phone_number: doc1.phone_number});
        if (doc2) {
                list.push(doc1);
        }
    });
print(list);

Upvotes: 0

Views: 101

Answers (2)

Saurabh Agrawal
Saurabh Agrawal

Reputation: 7739

You can use $in

Try this:

db.main_hikanshou.find({},{phone_number:1} ).toArray(function (_err, docs) {//get all phone phone numbers
    let phone_numberList = docs.map(v => { return v.phone_number });
    var doc2 = db.main_kokyaku.find({ phone_number: { "$in": phone_numberList } });//search phone 
    print(doc2);
})

Upvotes: 1

krbalaji
krbalaji

Reputation: 474

Join the matching phone_number to the main_hikanshou collection using aggregate

db.collection('main_hikanshou').aggregate([
    { $lookup:
        {
           from: 'main_kokyaku',
           localField: 'phone_number',
           foreignField: 'phone_number',
           as: 'phoneNumber'
        }
    }
])

Upvotes: 1

Related Questions