Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

javascript - How does mongodb create database/collection on the fly

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...