Skip to content

Instantly share code, notes, and snippets.

@DarrenSem
Last active November 2, 2023 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarrenSem/ebc63a63f214b9dc33ec85c9ae028684 to your computer and use it in GitHub Desktop.
Save DarrenSem/ebc63a63f214b9dc33ec85c9ae028684 to your computer and use it in GitHub Desktop.
MongoDB - function getDocumentArray( fnEachDocument, ...documents ) - returns cloned Array of passed documents (after running function on each)
// MongoDB - function getDocumentArray( fnEachDocument, ...documents ) - returns cloned Array of passed documents (after running function on each)
// Purpose: returns cloned Array of passed documents (after running function on each)
// Usage: const docArray = getDocumentArray( fnEachDocument, ...documents ); await coll.insertMany(docArray);
// see the docs for https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany
// esp. "_id Field" https://www.mongodb.com/docs/manual/reference/method/db.collection.insertMany/#_id-field
// ^ because DURING THE .insert[Many|One]() call, "_id" field is ADDED to the original document! (if missing)
const getDocumentArray = ( fnEachDocument, ...documents ) => (
fnEachDocument ||= ( document => document ),
[documents].flat(Infinity).map( obj => fnEachDocument( { ...obj } ) )
);
const getDocumentArray_test = async (coll) => {
// debugger;
const doc1 = {n: 1}, doc2 = {n: 2}, doc3 = {n: 3}, doc4 = {n: 4};
console.log(doc1, doc2, doc3, doc4); // {n: 1} {n: 2} {n: 3} {n: 4}
console.assert( !doc1._id && !doc2._id && !doc3._id, '^ All 3 docs have no "_id" field BEFORE .insertMany()' );
const resultsObject = await coll.insertMany( getDocumentArray( d => ( d.extraField = new Date(), d ), doc1, [doc2, doc3] ) );
console.assert( resultsObject.insertedCount === 3, 'Inserted 3 docs into collection' );
console.log(doc1, doc2, doc3, doc4); // {n: 1} {n: 2} {n: 3} {n: 4}
console.assert( !doc1._id && !doc2._id && !doc3._id, '^ All 3 docs have no "_id" field AFTER .insertMany( <using getDocumentArray()> )' );
const dtBeforeInsert = new Date();
console.log(+dtBeforeInsert, dtBeforeInsert);
const exampleManyWithout = await coll.insertMany( [doc4] );
console.assert( exampleManyWithout.insertedCount === 1, 'Inserted 1 doc (#4) via coll.insertMany( [doc4] )' );
console.log(doc1, doc2, doc3, doc4); // {n: 1} {n: 2} {n: 3} {n: 4, _id: new ObjectId("64cdef441122334455aaff03")}
console.assert( doc4._id, '^ BUT 4TH DOC now has "_id" field AFTER .insertMany( [doc4] )' );
console.log( +doc4._id.getTimestamp(), doc4._id.getTimestamp() );
console.assert( dtBeforeInsert < +doc4._id.getTimestamp() + 1001, '^ dtBeforeInsert < 4th doc "_id" .getTimestamp() + 1001' );
const exampleOneWithout = await coll.insertOne(doc3);
console.assert( exampleOneWithout.acknowledged === true, 'Inserted 1 doc (#3) via coll.insertOne(doc3)' );
console.log(doc1, doc2, doc3, doc4); // {n: 1} {n: 2} {n: 3, _id: new Object("64cdef441122334455aaff33")} {n: 4, _id: new ObjectId("64cdef441122334455aaff03")}
console.assert( doc3._id, '^ BUT 3TH DOC now has "_id" field AFTER .insertOne(doc3)' );
console.log( +doc3._id.getTimestamp(), doc3._id.getTimestamp() );
console.assert( dtBeforeInsert < +doc3._id.getTimestamp() + 1001, '^ dtBeforeInsert < 3rd doc "_id" .getTimestamp() + 1001' );
const exampleOneSimilarlyFixed = await coll.insertOne( { ...doc2, extraField: new Date() } );
console.assert( exampleOneSimilarlyFixed.acknowledged === true, 'Inserted 1 doc (#2) via coll.insertOne( { ...doc2, extraField: new Date() } )' );
console.log(doc1, doc2, doc3, doc4); // {n: 1} {n: 2} {n: 3, _id: new Object("64cdef441122334455aaff33")} {n: 4, _id: new ObjectId("64cdef441122334455aaff03")}
console.assert( !doc1._id && !doc2._id, '^ First 2 docs STILL have no "_id" field after final .insertOne( { ...doc2 } )' );
// debugger;
};
// NOTE: this function might be overkill for simple inserts -- but beware:
const AVOID_THIS_BecauseEachDocumentSTILLaddsThe_idField = await coll.insertMany( [ ...myDocArray ] ); // ditto for ( myDocArray.slice(0) );
const UseTHISinstead_ForDocumentArray = await coll.insertMany( myDocArray.map( document => ( { ...document, extraField: new Date() } ) ) );
const and_ALWAYS_USE_THIS_ForSingleDocument = await coll.insertOne( { ...myDoc, extraField: new Date() } );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment