|
e文:http://docs.mongodb.org/manual/core/shell-types/
Data Types in the mongo Shell
MongoDB
BSON provide support for additional data types thanJSON.
Drivers provide nativesupport for these data types in host languages and themongo
shell also provides several helper classes to supportthe use of these data types in the
mongo JavaScriptshell. See
MongoDB Extended JSON for additionalinformation.
Date
The
mongo shell provides various options to return the date,either as a string or as an object:
- Date() method which returns the current date as a string.
- Date() constructor which returns an
ISODate object when usedwith the
new operator.
- ISODate() constructor which returns an
ISODate object whenused with
or without the new operator.
Consider the following examples:
To return the date as a string, use the
Date() method, as in thefollowing example:
var myDateString = Date();
To print the value of the variable, type the variable name in theshell, as in the following:
myDateString
The result is the value of myDateString:
Wed Dec 19 2012 01:03:25 GMT-0500 (EST)
To verify the type, use the typeof operator, as in thefollowing:
typeof myDateString
The operation returns string.
To get the date as an ISODate object, instantiate a new instanceusing the
Date() constructor with the
new operator, as in thefollowing example:
var myDateObject = new Date();
To print the value of the variable, type the variable name in theshell, as in the following:
myDateObject
The result is the value of myDateObject:
ISODate("2012-12-19T06:01:17.171Z")
To verify the type, use the typeof operator, as in thefollowing:
typeof myDateObject
The operation returns object.
To get the date as an ISODate object, instantiate a new instanceusing the
ISODate() constructor
without the new operator,as in the following example:
var myDateObject2 = ISODate();
You can use the new operator with the
ISODate() constructoras well.
To print the value of the variable, type the variable name in theshell, as in the following:
myDateObject2
The result is the value of myDateObject2:
ISODate("2012-12-19T06:15:33.035Z")
To verify the type, use the typeof operator, as in thefollowing:
typeof myDateObject2
The operation returns object.
ObjectId
The
mongo shell provides the
ObjectId() wrapper classaround
ObjectId data types. To generate a new ObjectId, usethe following operation in the
mongo shell:
new ObjectId
See
ObjectId for full documentation of ObjectIds inMongoDB.
NumberLong
By default, the
mongo shell treats all numbers asfloating-point values. The
mongo shell provides theNumberLong() class to handle 64-bit integers.
The NumberLong() constructor accepts the long as a string:
NumberLong("2090845886852")
The following examples use the NumberLong() class to write to thecollection:
db.collection.insert( { _id: 10, calc: NumberLong("2090845886852") } )
db.collection.update( { _id: 10 },
{ $set: { calc: NumberLong("2555555000000") } } )
db.collection.update( { _id: 10 },
{ $inc: { calc: NumberLong(5) } } )
Retrieve the document to verify:
db.collection.findOne( { _id: 10 } )
In the returned document, the calc field contains aNumberLong object:
{ "_id" : 10, "calc" : NumberLong("2555555000005") }
If you use the
$inc to increment the value of a field thatcontains a
NumberLong object by a
float, the data type changesto a floating point value, as in the following example:
Use
$inc to increment the
calc field by
5, which themongo
shell treats as a float:
db.collection.update( { _id: 10 },
{ $inc: { calc: 5 } } )
Retrieve the updated document:
db.collection.findOne( { _id: 10 } )
In the updated document, the calc field contains a floatingpoint value:
{ "_id" : 10, "calc" : 2555555000010 }
NumberInt
By default, the
mongo shell treats all numbers asfloating-point values. The
mongo shell provides theNumberInt() constructor to explicitly specify 32-bit integers.
|
|