MongoDB:2个集合的primefaces调用(find + insert)

我怎么能自动获得最新的“回合”loggingObjectId并使用它插入到“存款”集合?

这个post的回答说不能这样做: 有没有办法自动更新MongoDB中的两个集合? 这是真的吗?

在过程A中,我想自动查找最新的一轮id(rid)并将其插入到存款中。 比赛的条件是,在A发现之后,另一个B可能会插入一轮,所以现在A有一个不是最新的,但是落后的1。 A怎么能轮streamfind摆脱这个摆脱存款(作用于这两个集合)primefaces?

// GET ROUND ID (RID) OF LATEST var rid; db.rounds.find().limit(1).sort({$natural:-1}, function(err, latestInsertedRound){ rid = latestInsertedRound[0]._id; print(rid, 'rid'); // if another process B inserts now, this rid is 1 behind // INSERT INTO DEPOSITS db.deposits.insert({uid:uid, iid:iid, rid:rid}, function(err, insertedDeposit){ print(insertedDeposit, 'insertedDeposit'); }); }); 

在Mongodb中插入文档有一个可以使用的callback函数。 这个callback函数有第二个参数返回插入的文档。

我尝试使用console.log打印第二个参数。 看起来像 :

 { result: { ok: 1, n: 1 }, ops: [ { username: 'user1', password: 'password1', _id: 562099bae1872f58b3a22aed } ], insertedCount: 1, insertedIds: [ 562099bae1872f58b3a22aed ] } 

insertedIds是保存插入的文档或文档的_id的数组。

因此,您可以在第一个集合的插入函数callback中将您的对象插入到第二个集合中。 有点混乱。

简单来说:将文档插入到第一个集合中。 在callback中,将文档插入到第二个集合中。

 MongoClient.connect(MONGOLAB_URI, function (err, db) { if (err) console.log("We have some error : " + err); else { db.createCollection('rounds', function (err, rounds) { if (err) { console.log('Error while creating rounds collection'); throw err; } else { rounds.insert({ 'username' : 'user1', 'password' : 'password1' }, function(err,docsInserted){ console.log('Last document inserted id :', docsInserted.insertedIds[0]); //inserting the document in the function callback db.createCollection('deposits', function (err, deposits) { if (err) { console.log('Error while creating deposits collection'); throw err; } else { //change array index according to your need //you may be inserting multiple objects simultaneously deposits.insert({'last_inserted_object' : docsInserted.insertedIds[0]); console.log('inserted into deposits collection'); } }); }); } }); } }); 

看来不可能在MongoDB的2个集合上primefaces操作,正如在这个答案中所解释的:

有没有办法自动更新MongoDB中的两个集合?

我留下的问题,因为它有一个稍微不同的重点(不是2更新,但find+插入)。

Interesting Posts