Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb translation for sql INSERT...SELECT

Tags:

mysql

mongodb

How to simply duplicate documents from collectionABC and copy them into collectionB if a condition like {conditionB:1} and add a timestamp like ts_imported - without knowing the details contained within the original documents?

I could not find a simple equivalent for mongodb which is similar to mysql's INSERT ... SELECT ...

like image 900
ledy Avatar asked Nov 23 '12 17:11

ledy


1 Answers

You can use javascript from mongoshell to achieve a similar result:

db.collectionABC.find({ conditionB: 1 }).
forEach( function(i) { 
  i.ts_imported = new Date();
  db.collectionB.insert(i);
});
like image 95
Eric Avatar answered Sep 25 '22 08:09

Eric