条件插入Mongodb

时间:2015-07-22 12:42:14

标签: java mongodb

我想在Collection1中插入数据取决于Collection2中存储的数据 但我不知道如何在MondoDB中做到这一点? 我想要像这个SQL代码:

insert into collection1 VALUES(...)  
WHERE
  ((select current from collection2 where id='1')+1)
    < (select max from collection2 where id='1')

我如何在MondoDB中做到这一点?

1 个答案:

答案 0 :(得分:0)

Eric的代码视为answer,几乎相似question

 db.collectionABC.find({ conditionB: 1 }).
forEach( function(i) { 
  i.ts_imported = new Date();
//write your logic here and insert modified document or a new document.
  db.collectionB.insert(i);
});