2016-08-07 4 views
0

나는 문학의 다양한 비트를 읽었습니다, 나는몽고 스킨 및 대량 작업? (2.1.0 및 2.2.0을 mongoskin MongoDB를 3.2)

https://stackoverflow.com/a/25636911

에서 질문자가 보는 것과 같은 문제를보고 있어요.

내 코드는 다음과 같습니다

coll = db.collection('foobar'); 
bulk = coll.initializeUnorderedBulkOp(); 

for entry in messages { 
    bulk.insert(entry); 
} 

bulk.execute(function (err, result) { 
    if (err) throw err 
    inserted += result.nInserted 
}); 

대부분은

bulk.execute이

유래 문제의 답을 정의되지 않습니다 잘 작동 bulk.insert 객체

입니다 고 말했다. "db.collection()의 callback flavor 만 작동하므로 다음을 시도했다.

db.collection('foobar', function (err, coll) { 
    logger.debug "got here" 
    if (err) throw err 
    bulk = coll.initializeUnorderedBulkOp() 
    ... same code as before 

db.collection()의 "callback flavor"가 3.0에서 삭제되었다는 것을 의미하는 "절실히"여기지는 않습니까?

불행히도 내 파이썬은 JS 프로토 타입 기술보다 낫다. 그래서 스킨 소스 코드를 보는 것이 나에게 의미가 없다.

대량 작업을 수행하는 mongoskin 2.1.0 및 2.2.0 mongodb JS 드라이버의 올바른 방법은 무엇입니까? 아니면이 기능이 전혀 구현되지 않았습니까?

답변

0

적어도 두 개의 답변이 있습니다

(1) 인서트를 사용하지만, 배열 형식, 그래서 당신은 하나의 호출로 여러 문서를 삽입합니다. 매력처럼 작동합니다.

(2) 대량 작업이 정말로 필요한 경우 mongoskin을 기본 mongo 인터페이스로 전환해야하지만 한 번의 호출만으로 전환해야합니다. 이 mongoskin에서 개인 인터페이스를 사용하고 있기 때문에 좀 짜증뿐만 아니라 mongoskin을 고수하는 가장 효율적인 방법 :

(커피 스크립트 예) 당신이 원하는 경우

하는
// bulk write all the messages in "messages" to a collection 
// and insert the server's current time in the recorded field of 
// each message 

// use the _native interface and wait for callback to get collection 
db._native.collection collectionName, (err, collection) -> 
    bulk = collection.initializeUnorderedBulkOp() 
    for message in messages 
     bulk.find 
      _id: message._id 
     .upsert().updateOne 
      $set: message 
      $currentDate: 
       recorded: true 
    bulk.execute (err, result) -> 
     // ... error and result checking code 

(3) 구현 $하는 currentDate 및되지 않은 일반적인 대량 작업이 인수없이 (1) 솔루션을 참조하지만,하지-매우 잘 문서화 BSON 개체 타임 스탬프()를 사용하는 것이 :

for msg in messages: 
    msg.recorded = Timestamp() 
db.mycollection.insert(msg) 

대량 삽입을하고 설정되는 DB 서버의 시간에 대한 타임 스탬프 레코드가 db에 기록 될 때.