IgeMongoDb & incremental update

Post your help queries and engine programming questions here

IgeMongoDb & incremental update

Postby stokaboka » Wed Jul 02, 2014 9:03 am

Hi!

to perform incremental update i write updateJson parameter:

Code: Select all
{$inc: {coins: __b_inc_coins}}


in the source code IgeMongoDb (line 306):
Code: Select all
// Ensure we only update, not overwrite!
finalUpdateJson = {
    '$set': updateJson
};


$set operator does not allow operators such as $inc

as a temporary solution I made (line 311):
Code: Select all
// Got the collection (or err)
 tempCollection.update(searchJson, updateJson, options,  function (err, results) {


but this is not the right solution...

how to properly use the operators $inc and others like him?
stokaboka
 
Posts: 14
Joined: Fri Nov 08, 2013 6:13 am

Re: IgeMongoDb & incremental update

Postby stokaboka » Wed Sep 17, 2014 1:58 pm

did something like this:
(using "componentId: 'mongo'" leads to an expansion module mongodb )
Code: Select all
/**
 * Created by stokaboka on 14.07.2014.
 */

var MyMongoDbComponent = IgeMongoDbComponent.extend({
    classId: 'MyMongoDbComponent',
    componentId: 'mongo',

    init: function (entity, options) {
        IgeMongoDbComponent.prototype.init.call(this, entity, options);
    },

    x_mongo_test: function(){
        console.log("MyMongoDbComponent ready");
    },

    /**
     * Performs a database update operation which will only update the fields
     * of records that match the searchJson object fields, with the corresponding
     * fields in the updateJson object. It will NOT overwrite the updated document
     * with only the fields in the updateJson object.
     * @param {String} coll The collection name to operate on.
     * @param {Object} searchJson The object containing the fields
     * that a record must match to be updated.
     * @param updateJson The object containing the fields to update
     * matching records with.
     * @param {Function} callback The method to call once the DB operation
     * has been completed.
     * @param {Object} options The options object containing three boolean
     * values: safe, multi and upsert. See the MongoDB docs for more information.
     */
    x_update: function (coll, searchJson, updateJson, callback, options) {
        var self = this;

        // Set some options defaults
        options = options || {
            safe: true,
            multi: true,
            upsert: false
        };

        this.client.collection(coll, function (err, tempCollection) {
            if (!err) {
//                var finalUpdateJson;

                self.collectionIdToId(coll, searchJson);
                self.collectionIdToId(coll, updateJson);

//this code does not allow operators $inc etc.   !!!!!!
                // Ensure we only update, not overwrite!
//                finalUpdateJson = {
//                    '$set': updateJson
//                };

                // Got the collection (or err)
                tempCollection.update(searchJson, updateJson, options,  function (err, results) {
                    // Got the result cursor (or err)
                    // Callback the results
                    if (typeof(callback) === 'function') {
                        callback.apply(self, [err, results]);
                    }
                });
            } else {
                self.log('Mongo cannot run a findAll on collection ' + coll + ' with error: ' + err, 'error', tempCollection);
            }
        });
    }

});

if (typeof(module) !== 'undefined' && typeof(module.exports) !== 'undefined') { module.exports = MyMongoDbComponent; }


includes modules on the server (ServerConfig.js):
Code: Select all
...
{name: 'MyMongoDbComponent', path: './gameClasses/MyMongoDbComponent'},
db: {
   type: 'mongo',
   host: 'localhost',
   user: '***',
   pass: '***',
   dbName: 'test'
}
...
stokaboka
 
Posts: 14
Joined: Fri Nov 08, 2013 6:13 am


Return to Help & Questions

Who is online

Users browsing this forum: No registered users and 1 guest
cron