Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to override default functions in Mongodb?

So what I want to do is make findOne work more like it does in Meteor, but through the Mongo shell. In short I want to be able to do something like this db.collection.findOne("thisIsAnId") and have it lookup that id in that collection.

I tried loading a file that has this in it...

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 originalFindOne would just chain to the default findOne, this didn't work at all. So after having no luck finding a way to override a default function I thought maybe I could create a new function like db.collection.simpleFindOne() or something, but I can't find a way to attach it to the mongo shell so that it will be available to any collection.

Anyone have some insight on how mongo internals work that could give me some help?

like image 985
Shaded Avatar asked Dec 11 '14 15:12

Shaded


People also ask

Can we override _ID in MongoDB?

The _id field is immutable—that is, once a document exists in your MongoDB system, it has, by definition, been assigned an _id, and you cannot change or update its primary key. That said, _id can be overridden when you insert new documents, but by default it will be populated with an ObjectID.

How do I replace a whole document in MongoDB?

In MongoDB, you are allowed to replace an existing document with a new document in the collection with the help of db. collection. replaceOne() method. This method will replace the existing document with the replacement document.

What is Dbpath in MongoDB?

By default, MongoDB listens for connections from clients on port 27017 , and stores data in the /data/db directory. On Windows, this path is on the drive from which you start MongoDB. For example, if you do not specify a --dbpath , starting a MongoDB server on the C:\ drive stores all data files in C:\data\db .

How do I change the value of a field in MongoDB?

We can use $set and $inc operators to update any field in MongoDB. The $set operator will set the newly specified value while the $inc operator will increase the value by a specified value. Let's first look into the MongoDB query to update two fields of the employee collection using the $set operator: db.


1 Answers

Try adding this snippet to one of your Mongo config files:

(function() {

// Duck-punch Mongo's `findOne` to work like Meteor's `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);
  };
}

})();

I originally found the DBCollection object/class by typing db.myCollection.constructor into the Mongo shell. I then confirmed that findOne was defined on its prototype by verifying that (a) DBCollection was globally accessible, (b) that DBCollection.prototype existed, and (c) that typeof DBCollection.prototype.findOne === "function" (much like the snippet does).


Edit: Added a logic branch to also cover ObjectId-based IDs.

like image 65
James M. Greene Avatar answered Oct 11 '22 18:10

James M. Greene