2

I am planning on transforming a large data set from my mongo database and creating a new collection from the transformed documents. I wanted to do this as part of a single aggregation to stop round trips back and forth from the server and the database. Our application automatically enforces String type ID's, so I cannot use the automatic _ids generated by $out / $merge as these are ObjectIds()

Obviously I can pull the data down and generate a unique string ID for each document then insert them but I was hoping for a way to do this within a single aggregation query. Any ideas?

1 Answers1

0

Take a look here how to $project ObjectId to string value in mongodb aggregate?

We do pretty much the same thing in our mongo scripts:

db.YourCollection.insert({
    _id: new ObjectId().valueOf(),
    ...fields
});
tuxonator
  • 66
  • 2
  • 2
    Thanks for the respond but I tried this before but as it's a bulk insertion with multiple documents the ObjectId was the same for each one. Seems like the newly generated ID gets sent up with the initial query as a singular value rather than being created with each new document. Was I doing something wrong? Would be great if this works and I just missed something – JamesVitaly Jul 11 '20 at 08:58