markType() - Specifies the data type

Specifies a data type for a JSON field in a collection.

Syntax diagram

Read syntax diagramSkip visual syntax diagramdb.collection. markType({field: type})

Command parameters

field
This string argument specifies an attribute name.
type
This argument specifies a data type, or null to remove the data type. The following data types are supported:
  • $int
  • $long
  • $number
  • $date
  • $timestamp
  • $string: length
  • $binary: length

Example

Example 1: Mark the 'x' field as integer type, and sort by 'x' in ascending order.
db.c.insert({x:-100})
db.c.insert({x:-10})
db.c.markType({x:"$int"})
db.c.find().sort({x:1})
Sample output:
Row 1:
{
"_id":{"$oid":"51f08bbdeefadf1d37d08bd9"},
"x":-100
}
Row 2:
{
"_id":{"$oid":"51f08bc3eefadf1d37d08bda"},
"x":-10
}
Example 2: Mark the 'x' field as string type, and sort by 'x' in ascending order
db.c.markType({x:["$string",20]})
db.c.find().sort({x:1})
Sample output:
Row 1:
{
"_id":{"$oid":"51f08bc3eefadf1d37d08bda"},
"x":-10
}
Row 2:
{
"_id":{"$oid":"51f08bbdeefadf1d37d08bd9"},
"x":-100
}