sql >> Base de Datos >  >> NoSQL >> MongoDB

buscar por número de tipo $ en mongodb

Solo hay un tipo numérico en JavaScript (Number ), que se representa en binario como un número de punto flotante IEEE 754 (doble).

En las especificaciones BSON esto se representará como un doble (tipo 1), por lo que debería poder encontrarlo con:

db.people.find({name: { $type: 1 }})

Hay algunos mongo ayudantes de shell si desea insertar diferentes tipos de datos BSON tipos :

42              // Type 1:  double (64-bit IEEE 754 floating point, 8 bytes)
NumberInt(42)   // Type 16: int32  (32-bit signed integer, 4 bytes)
NumberLong(42)  // Type 18: int64  (64-bit signed integer, 8 bytes)

Entonces, por ejemplo:

db.people.insert({ name: 'default', num: 42 })
db.people.insert({ name: 'NumberLong', num: NumberLong(42) })
db.people.insert({ name: 'NumberInt', num: NumberInt(42) })

Las diferentes representaciones numéricas aún coincidirán si haces find() en un número que se puede representar en múltiples formatos (es decir, un entero de 32 bits también se puede representar como un doble o int64).

Por ejemplo:

db.people.find({num:42})
{
    "_id" : ObjectId("50965aa3038d8c8e85fd3f45"),
    "name" : "default",
    "num" : 42
}
{
    "_id" : ObjectId("50965aa3038d8c8e85fd3f46"),
    "name" : "NumberLong",
    "num" : NumberLong(42)
}
{
    "_id" : ObjectId("50965aa3038d8c8e85fd3f47"),
    "name" : "NumberInt",
    "num" : 42
}

Sin embargo, si encuentra por $type , la representación BSON es diferente:

> db.people.find({num: { $type: 1 }})
{
    "_id" : ObjectId("50965aa3038d8c8e85fd3f45"),
    "name" : "default",
    "num" : 42
}

> db.people.find({num: { $type: 16 }})
{
    "_id" : ObjectId("50965aa3038d8c8e85fd3f47"),
    "name" : "NumberInt",
    "num" : 42
}

> db.people.find({num: { $type: 18 }})
{
    "_id" : ObjectId("50965aa3038d8c8e85fd3f46"),
    "name" : "NumberLong",
    "num" : NumberLong(42)
}