Giter VIP home page Giter VIP logo

Comments (8)

ZJONSSON avatar ZJONSSON commented on September 10, 2024

Thanks @valeeum, this would be a very nice improvement. Here is a suggested implementation: If $setOnInsert is provided in the options of etl.mongo.update then:

  • if its an Object, then the same Object will be included as $setOnInsert for every data packet in the bulk update
  • if its a function it will be called with the data for each packet and the results will be included in the corresponding $setOnInsert

So if you wanted to set dateCreated you could either do: etl.mongo.update(collection,keys,{upsert:true, $setOnInsert: new Date()}) (which will set all inserted records to the date when the bulk started) or etl.mongo.update(collection,keys,{upsert:true, $setOnInsert: function() { return new Date();}}) which would set it to the specific time each record was included in a bulk.

Let me know what you think or if you have other ideas?

from node-etl.

valeeum avatar valeeum commented on September 10, 2024

sorry about the delayed response...i was out of town.

i love the current minimalistic api but would like to have the additional flexibility to completely modify the argument passed in to the op.updateOne function.

op.updateOne({$set:d});

If the callback were supplied with the d variable as an argument, that would give the user the flexibility to craft whatever update operation they would like. $set is just one of many operators available to us. Comprehensive list of field update operators are provided here:

https://docs.mongodb.com/manual/reference/operator/update-field.

from node-etl.

ZJONSSON avatar ZJONSSON commented on September 10, 2024

Ah that makes sense - how about adding a new generic function etl.mongo.bulk that simply passes on each incoming object directly to .updateOne without wrapping it inside $set. The keys of each objects would have to be any of the operators listed in the reference above? Let me know if you have better ideas?

from node-etl.

valeeum avatar valeeum commented on September 10, 2024

That sounds great! How would we differentiate a bulk insert vs bulk update (as well as the option to upsert)?

from node-etl.

valeeum avatar valeeum commented on September 10, 2024

Take a look at the changes I made here...

https://github.com/vcomedia/node-etl/blob/master/lib/mongo/update.js

I actually modified the existing etl.mongo.update function to allow flexibility to accommodate this use case as well as allow for backward compatibility (backward compatibility not yet tested but theoretically should work).

So we can now support feeding the following object to the function....

{
	$setOnInsert: {
		dateCreated: new Date(),
	},
	$set: {
		dateModified: new Date()
	},
	$push: {
		someArray: {
                   id: 'someID',
                   type: 'someType'
                }
	}
}

with the following keys...

['$push.someArray.id','$push.someArray.type']

The function will now walk the object using the dotted string notation and discard the first component starting with '$' character when creating the criteria since that signifies an update operation. So in this case, since the the keys start with an array operator ($push or $addToSet), the resulting query will be:

{
	someArray : {
               $elemMatch: { 
                     id: 'someID',
                     type: 'someType'
               }
        }
}

$elemMatch is used instead of equality to prevent any issues with upsert operations. You can still use objects that represent property + values like the previous version using keys that don't include update operators. Only difference is that now there is support for dot notation for the keys.

Let me know what you think.

from node-etl.

ZJONSSON avatar ZJONSSON commented on September 10, 2024

Sorry for the delay - I'm back at my desk after the holidays and will take a better look in the next couple of days. Happy new year!

from node-etl.

valeeum avatar valeeum commented on September 10, 2024

I want to note I made one more update to address an error in mongo 3.4 caused by passing in the entire options variable into the bulk execute method. The new format I propose is to explicitly set the writeConcern object. This of course will break backward compatibility.

      var writeConcern = self.options.writeConcern  ? self.options.writeConcern  : null;
      bulk.execute(writeConcern,function(err,d) {
        cb(err,self.options.pushResult && d);
      });

Spec is available here:

https://docs.mongodb.com/manual/reference/method/Bulk.execute/

The entire update is available here:

https://github.com/vcomedia/node-etl/blob/master/lib/mongo/update.js

from node-etl.

ZJONSSON avatar ZJONSSON commented on September 10, 2024

Thanks again @valeeum - I folded your code into etl.mongo.bulk to make it available for use.
I was a little hesitant to change the update as this would break backwards compatibility.
We need to add tests and docs, and when we do a major release we could merge with update

published as + [email protected]

from node-etl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.