在mongodb中按$ type号查找 [英] find by $type number in mongodb

查看:94
本文介绍了在mongodb中按$ type号查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件

> db.people.insert({name: "Bob"})
> db.people.insert({name: 42})

如果需要按类型String查找,可以使用:

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

2是字符串的类型号

但是我怎么能按类型Number查找?

> typeof db.people.findOne({name: 42}).name
number

number .

更新:我尝试按16种和18种类型进行查找.这是行不通的.

> db.people.find({ name: 42 })
{ "_id" : ObjectId("509645ae5013826ee61d96ac"), "name" : 42 }
> db.people.find({name: { $type: 16 }})
> db.people.find({name: { $type: 18 }})

解决方案

只有一种数值类型在JavaScript(Number)中,以二进制形式表示为IEEE 754浮点数(双精度).

BSON规范中,它将被表示为双精度(类型1),因此您应该能够找到:

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

如果您想插入其他BSON mongo shell助手. >数据类型:

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)

例如:

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

如果对可以以多种格式表示的数字执行find(),则不同的数字表示形式仍将匹配(即32位整数也可以表示为double或int64).

例如:

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
}

但是,如果通过$type查找,则BSON表示形式是不同的:

> 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)
}

I've got following documents

> db.people.insert({name: "Bob"})
> db.people.insert({name: 42})

If I need find by type String I can use:

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

Where 2 is Type Number of String

But how could I find by type Number?

> typeof db.people.findOne({name: 42}).name
number

There is no type number in possible types.

upd: I've tried to find by 16 and 18 types. It's not works.

> db.people.find({ name: 42 })
{ "_id" : ObjectId("509645ae5013826ee61d96ac"), "name" : 42 }
> db.people.find({name: { $type: 16 }})
> db.people.find({name: { $type: 18 }})

解决方案

There is only one numeric type in JavaScript (Number), which is represented in binary as an IEEE 754 floating point number (double).

In the BSON spec this will be represented as a double (type 1), so you should be able to find with:

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

There are some mongo shell helpers if you want to insert different BSON data types:

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)

So for example:

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

The different numeric representations will still match if you do a find() on a number that can be represented in multiple formats (i.e. a 32-bit integer can also be represented as a double or int64).

For example:

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
}

However if you find by $type, the BSON representation is different:

> 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)
}

这篇关于在mongodb中按$ type号查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆