: isolated: Prevents a write operation that affects multiple documents from yielding to other reads or writes once the first document is written... $isolated operator causes write operations to acquire an exclusive lock on the collection...will make WiredTiger single-threaded for the duration of the operation.
あなたが複数のドキュメントを更新しているのであれば、あなたは最初に、サードパーティのAPIからデータを取得し、たとえば配列に情報を解析して、モンゴシェル内でこのようなものを使用することができます
db.foo.update(
{ status : "A" , $isolated : 1 },
{ $set: { < your key >: < your info >}}, //use the info in your array
{ multi: true }
)
また、ドキュメントを1つずつ更新する必要がある場合は、MongoDBのノードドライバのfindAndModify()またはupdateOne()を使用できます。 MongoDB documentation 'に注意してください。単一のドキュメントを変更する場合、findAndModify()メソッドとupdate()メソッドの両方がドキュメントをアトミックに更新します。
一つ一つを更新する例:最初にあなたconnect to the Mongod with the NodeJS driver、その後connect to the third part API using NodeJS's Request module, for example、modify your documentsにデータを使用する前に、データを取得し、解析し、以下のようなもの:
var request = require('request');
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
var collection = db.collection('simple_query');
collection.find().forEach(
function(doc) {
request('http://www.google.com', function(error, response, body) {
console.log('body:', body); // parse body for your info
collection.findAndModify({
<query based on your doc>
}, {
$set: { < your key >: < your info >
}
})
});
}, function(err) {
});
});
アプリケーション層で[悲観的なロック](https://en.wikipedia.org/wiki/Record_locking)を実装する必要があります。 –
件名を詳しく記入して回答を投稿できますか? – Azarus
申し訳ありませんが、詳細は何もありません。 Mongodbはトランザクションをサポートしていないので、自分で実装するか、npmパッケージを使用する必要があります。前者はStackOverflow形式では広すぎ、後者は非常に少数であるためかなり意見があります。あなたがパッケージの1つを試して、いくつかのコーディングの問題に直面するなら、それはより適切な質問でしょう。 –