最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is "JavaScript with Scope" in MongoDB - Stack Overflow

programmeradmin7浏览0评论

In the following link / It mentions JavaScript with Scope is a possible data type in documents.

My questions are:

(1) What is a JaveScript with scope?

(2) Is it some kind of "Internal" data types in MongoDB

By “internal”, I mean it cannot be used by users. I didn't find any more info about this type, except it is mentioned in above link

(3) In mongo c driver, I found Struct bson_value_t .0.0/bson_value_t.html what is the "scope_data" buffer ?

In the following link https://docs.mongodb./manual/reference/bson-types/ It mentions JavaScript with Scope is a possible data type in documents.

My questions are:

(1) What is a JaveScript with scope?

(2) Is it some kind of "Internal" data types in MongoDB

By “internal”, I mean it cannot be used by users. I didn't find any more info about this type, except it is mentioned in above link

(3) In mongo c driver, I found Struct bson_value_t http://mongoc/libbson/1.0.0/bson_value_t.html what is the "scope_data" buffer ?

Share Improve this question asked Aug 25, 2016 at 21:51 Chang SuChang Su 912 bronze badges 2
  • Possible duplicate of What is the use of type javascript/javascriptwithscope of bson – Evan Trimboli Commented Aug 25, 2016 at 21:59
  • 4 @EvanTrimboli I found that when looking for duplicates also. The answer isn't helpful at all. – castletheperson Commented Aug 25, 2016 at 22:01
Add a ment  | 

1 Answer 1

Reset to default 11

Believe it or not, it's possible to store a "live" Javascript function in a MongoDB collection:

> db.collection.insert({ name: "add1", f: (function(x) { return x + 1 }) })
WriteResult({ "nInserted" : 1 })
> db.collection.findOne({ name: "add1" }).f(123)
124

A "function with closure" (or, more simply, a "closure") is a function which refers to variables which exist outside the function, like incrementX in the following snippet:

var x = 1;
function incrementX() { x++; }

These functions can be stored in a MongoDB collection as well; they will bind to the scope of the mongo session when they're executed:

> db.collection.insert({
    name: "incrementX",
    f: (function() { x++; })
})
WriteResult({ "nInserted" : 1 })
> var x = 123;
> db.collection.findOne({ name: "incrementX" }).f()
> x
124

For some unknowable reason, the BSON designers decided to use a different data type for Javascript functions depending on whether they were closed over any variables or not. The plain "Javascript" type is used for functions which don't close over any variables, and "Javascript (with scope)" is used for closures.


Why one would store a Javascript function in a MongoDB collection is… a good question. I'm not sure what the purpose of this feature is; it honestly seems rather dangerous and ill-advised to me. In particular, it'll be difficult to do anything useful with them if you're using a Mongo driver in a non-Javascript language, and using functions in the database exposes you to potential exploits if a malicious user is able to inject a function into your database. If I were you, I'd pretend this feature didn't exist and move on.

发布评论

评论列表(0)

  1. 暂无评论