What is the use of type javascript/javascriptwithscope of bson

浪尽此生 提交于 2020-01-14 19:48:06

问题


I am wondering about the use of these two type of bson (javascript/javascriptwithscope); as the base type of bson.

What's the use case of it and how to generate a javascriptwithscope object to save in mongodb?

Type Number Alias Notes

Double  1   “double”     
String  2   “string”     
Object  3   “object”     
Array   4   “array”  
Binary data 5   “binData”    
Undefined   6   “undefined” Deprecated.
ObjectId    7   “objectId”   
Boolean 8   “bool”   
Date    9   “date”   
Null    10  “null”   
Regular Expression  11  “regex”  
DBPointer   12  “dbPointer”  
JavaScript  13  “javascript”     
Symbol  14  “symbol”     
JavaScript (with scope) 15  “javascriptWithScope”    
32-bit integer  16  “int”    
Timestamp   17  “timestamp”  
64-bit integer  18  “long”   
Min key -1  “minKey”     
Max key 127 “maxKey”     

回答1:


Basically we need to do nothing :-) with data type as mongo engine will apply correct type to inserted data.

When creating a javascript object which will be inserted to mongo:

var object = {
    thisWillBeNumber : 1,
    thisWillBeString :"aaa",
    thisWillBeAnArray = [1,2,3]
    thisWillBeDateTime: new Date()  
}

then mongo uses javascript object type and savs it. In some drivers/framework we can enforce on application level types of our field/variables and such information could be added to stored document.



来源:https://stackoverflow.com/questions/37941368/what-is-the-use-of-type-javascript-javascriptwithscope-of-bson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!