2015-10-17 50 views
4

在Algolia的文檔中,node.js的一部分,他們指定要使用MySQL索引,但不MongoDB中,我有一個關於這個問題的另一個問題,但它更是一個普遍的問題,check here如何迭代node.js中的mongodb數據庫發送給Algolia?

有些人問我使用mongo-connector但試過了,我得到了一些未知的錯誤,這讓我到了第一個

我真正的問題是,我如何迭代mongodb到阿爾戈利亞的集合列表?

這是Algolia在Node.js的

var _ = require('lodash'); 
var async = require('async'); 
var mysql = require('mysql'); 

var algoliasearch = require('algoliasearch'); 
var client = algoliasearch("RQGLD4LOQI", "••••••••••••••••••••••••••••••••"); 
var index = client.initIndex('YourIndexName'); 

var connection = mysql.createConnection({ 
    host: 'localhost', 
    user: 'mysql_user', 
    password: 'mysql_password', 
    database: 'YourDatabaseName' 
}); 

connection.query('SELECT * FROM TABLE_TO_IMPORT', function(err, results, fields) { 
    if (err) { 
    throw err; 
    } 

    // let's use table IDS as Algolia objectIDs 
    results = results.map(function(result) { 
    result.objectID = result.id; 
    return result; 
    }); 

    // split our results into chunks of 5,000 objects, to get a good indexing/insert performance 
    var chunkedResults = _.chunk(results, 5000); 

    // for each chunk of 5,000 objects, save to algolia, in parallel. Call end() when finished 
    // or if any save produces an error 
    // https://github.com/caolan/async#eacharr-iterator-callback 
    async.each(chunkedResults, index.saveObjects.bind(index), end); 
}); 

function end(err) { 
    if (err) { 
    throw err; 
    } 

    console.log('MySQL<>Algolia import done') 
}; 

具體地說我用貓鼬作爲我的ORM版本的MySQL,所以我在其他圖書館沒有經驗。請幫我在這,這樣我就可以了一些搜索界面已經:(

回答

9

您可以使用下面的代碼來遍歷整個MongoDB的mydb.myCollection集合+創建將被髮送到Algolia指數批次:

var Db = require('mongodb').Db, 
    Server = require('mongodb').Server, 
    algoliasearch = require('algoliasearch'); 

// init Algolia index 
var client = algoliasearch("*********", "••••••••••••••••••••••••••••••••"); 
var index = client.initIndex('YourIndexName'); 

// init connection to MongoDB 
var db = new Db('mydb', new Server('localhost', 27017)); 
db.open(function(err, db) { 
    // get the collection 
    db.collection('myCollection', function(err, collection) { 
    // iterate over the whole collection using a cursor 
    var batch = []; 
    collection.find().forEach(function(doc) { 
     batch.push(doc); 
     if (batch.length > 10000) { 
     // send documents by batch of 10000 to Algolia 
     index.addObjects(batch); 
     batch = []; 
     } 
    }); 
    // last batch 
    if (batch.length > 0) { 
     index.addObjects(batch); 
    } 
    }); 
}); 
相關問題