11

Mongodb is cool enough to create the database/collection on the fly, if we run a code similar to

db.store.save({a: 789});

It automatically creates store collection and add a document to it.

My javascript understanding says it is not possible to call a method on an undefined property of db object. It should have resulted in some kind of error/exception.

I am curious to understand the happenings behind the scene and if there is any helpful link please point me to those. Googling did not help me much.

dopeddude
  • 4,943
  • 3
  • 33
  • 41
  • 1
    Although I do not know the answer to this question I believe MongoDB is developed in C++. What you use in Javascript is a driver, those drivers are developed in the language it's supposed to be used (someone correct me if I'm mistaken). The question persists though, I just thought it would be useful to note this. – martskins Jan 21 '15 at 14:35
  • Mongo is [open source](https://github.com/mongodb), so you could dig around in the [aforementioned JS driver](https://github.com/mongodb/node-mongodb-native) (if you have the time) to see if you can figure it out =) – Josh Darnell Jan 21 '15 at 14:57

2 Answers2

4

In JavaScript there is a way to define a function that will be executed when an undefined method is called.

Example:

var o = {
  __noSuchMethod__: function(id, args) { console.log(id, '(' + args.join(', ') + ')'); }
};

o.foo(1, 2, 3);
o.bar(4, 5);
o.baz();

// Output
// foo (1, 2, 3)
// bar (4, 5)
// baz ()

Note this is a non-standard feature and today only works in Firefox.

I do not know how MongoDB implemented this feature, but I'm just responding in order to report that can be done this way.

Fot more details see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/noSuchMethod

Leonardo Delfino
  • 1,488
  • 10
  • 20
  • 2
    FYI, MongoDB 2.4+ uses the [V8 JavaScript engine](http://docs.mongodb.org/manual/release-notes/2.4-javascript/). – Stennie Jan 22 '15 at 15:30
2

As I recall in a NodeJS environment you must do something like this to actually create a record: db.get('collectionName').insert({..something...}); or db.get('collectionName').save({...something...}); but you don't get to use the collection name as a property of db.

The line you're mentioning is only used in MongoDB shell, which is not Javascript. I guess you're misunderstanding what's MongoDB shell and what's a MongoDB driver.

So long story short MongoDB (driver) is not able to access an undefined property.

EDIT

In response to your comment..

MongoDB JS driver's GitHub page pretty much points out how to insert a field and always uses the syntax I mentioned: https://github.com/mongodb/node-mongodb-native

As for what you're using in the shell it's pretty clear that you can't just use Javascript in a command shell. So I guess I'll point you to a place in which you can see in what language was MongoDB developed: http://www.mongodb.org/ pretty much the first line says it's written in C++.

Hope this helps clarify your question

martskins
  • 2,920
  • 4
  • 26
  • 52
  • Yes and the answer seems like extension of the your comment on my question. Many Thanks. if possible can you please refer me to any link quoting the same. – dopeddude Jan 23 '15 at 11:22