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 Answer 1

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
});
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.