问题
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