mongodb translation for sql INSERT...SELECT

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);
});

I realise that this is an old question but...there is a better way of doing it now. MongoDB has now something called aggregation pipeline (v 3.6 and above, maybe some older ones too - I haven't checked). The aggregation pipeline allows you to do more complex things like perform joins, add fields and save documents into a different collection. For the OP's case, the pipeline would look like this:

var pipeline = [
    {$match: {conditionB: 1}},
    {$addFields: {ts_imported: ISODate()}},
    {$out: 'collectionB'}
]
// now run the pipeline
db.collectionABC.aggregate(pipeline)

Relevant docs:

  • Aggregation pipeline
  • $out stage
  • some important limits

Tags:

Mysql

Mongodb