Is there a way to override the default functions in Mongodb?

So, I want to do findOnemore than in Meteor, but through the Mongo shell. In short, I want to do something like this db.collection.findOne("thisIsAnId")and look for it in this collection.

I tried to upload a file that has this ...

db.collection.findOne = function(query, fields, options){
    if(typeof query === "string") {
        return db.collection.originalFindOne({_id : query}, fields, options);
    }

    return db.collection.originalFindOne(query, fields, options); 
}

Where it originalFindOnewill simply snap to default findOne, this does not work at all. Therefore, with no luck finding a way to override the default function, I thought that maybe I could create a new type function db.collection.simpleFindOne()or something else, but I can’t find a way to bind it to the mongo shell so that it is accessible to any collection .

Does anyone have an idea of ​​how the internal mango employees work that could help me?

+4
1

Mongo:

(function() {

// Duck-punch Mongo `findOne` to work like Meteor `findOne`
if (
  typeof DBCollection !== "undefined" && DBCollection &&
  DBCollection.prototype && typeof DBCollection.prototype.findOne === "function" &&
  typeof ObjectId !== "undefined" && ObjectId
) {
  var _findOne = DBCollection.prototype.findOne,
      _slice = Array.prototype.slice;
  DBCollection.prototype.findOne = function() {
    var args = _slice.call(arguments);
    if (args.length > 0 && (typeof args[0] === "string" || args[0] instanceof ObjectId)) {
      args[0] = { _id: args[0] };
    }
    return _findOne.apply(this, args);
  };
}

})();

/ DBCollection, db.myCollection.constructor Mongo. , findOne , , (a) DBCollection , (b) DBCollection.prototype (c), typeof DBCollection.prototype.findOne === "function" ( ).


: ​​ , ObjectId.

+4

All Articles