返回 MongoDB 中字段的实际类型

发布于 2024-09-08 09:51:30 字数 503 浏览 6 评论 0原文

在 MongoDB 中,使用 $type,可以根据字段是否与 BSON 数据类型匹配来过滤搜索(请参阅 DOCS)。

例如。

db.posts.find({date2: {$type: 9}}, {date2: 1})

返回:

{ 
    "_id" : ObjectId("4c0ec11e8fd2e65c0b010000"), 
    "date2" : "Fri Jul 09 2010 08:25:26 GMT" 
}

我需要一个查询来告诉我集合中每个字段的字段的实际类型是什么。 MongoDB 可以做到这一点吗?

In MongoDB, using $type, it is possible to filter a search based on if the field matches a BSON data type (see DOCS).

For eg.

db.posts.find({date2: {$type: 9}}, {date2: 1})

which returns:

{ 
    "_id" : ObjectId("4c0ec11e8fd2e65c0b010000"), 
    "date2" : "Fri Jul 09 2010 08:25:26 GMT" 
}

I need a query that will tell me what the actual type of the field is, for every field in a collection. Is this possible with MongoDB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

唐婉 2024-09-15 09:51:30

从 MongoDB 3.4 开始,您可以使用 $type 聚合运算符返回字段的类型。

db.posts.aggregate( 
    [ 
        { "$project": { "fieldType": {  "$type": "$date2"  } } } 
    ]
)

产生:

{ 
    "_id" : ObjectId("4c0ec11e8fd2e65c0b010000"), 
    "fieldType" : "string" 
}

Starting from MongoDB 3.4, you can use the $type aggregation operator to return a field's type.

db.posts.aggregate( 
    [ 
        { "$project": { "fieldType": {  "$type": "$date2"  } } } 
    ]
)

which yields:

{ 
    "_id" : ObjectId("4c0ec11e8fd2e65c0b010000"), 
    "fieldType" : "string" 
}
又怨 2024-09-15 09:51:30

中输入以下查询

  typeof db.employee.findOne().first_name

在 mongo shell语法

 typeof db.collection_name.findOne().field_name

type the below query in mongo shell

  typeof db.employee.findOne().first_name

Syntax

 typeof db.collection_name.findOne().field_name
何必那么矫情 2024-09-15 09:51:30

好的,以下是一些可能有帮助的相关问题:

使用 map-reduce 获取集合中的所有字段名称

这是列出所有可能字段的递归版本

希望这可以帮助您开始。但是,我怀疑您会遇到此请求的一些问题。这里有两个问题:

  1. 我找不到 JSON 的“gettype”函数。您可以通过 $type 进行查询,但看起来您实际上无法在字段上运行 gettype 函数并将其映射回 BSON 类型。
  2. 一个字段可以包含多种类型的数据,因此您需要一个计划来处理这个问题。即使这并不明显,Mongo 也可以将一些数字存储为整数,而其他数字则存储为浮点数,而您并不真正知道。事实上,使用 PHP 驱动程序,这是完全可能的。

因此,如果您假设您可以解决问题#1,那么您应该能够使用“获取所有字段名称”的细微变化来解决问题#2。

它可能看起来像这样:

"map" : function() { for (var key in this) { emit(key, [ typeof value[key] ]); } }
"reduce" : function(key, stuff) { return (key, add_to_set(stuff) ); }

所以基本上你会在映射函数中发出键值类型(作为数组)。然后,您可以从reduce 函数中为每种类型添加唯一的条目。

运行结束时,您将得到如下数据

{"_id":[255], "name" : [1,5,8], ... }

当然,这就是全部大量的工作,根据您的实际问题,您可能只想确保(从您的代码中)始终输入正确类型的数据。数据入库后查找数据类型绝对是一件痛苦的事情。

OK, here are some related questions that may help:

Get all field names in a collection using map-reduce.

Here's a recursive version that lists all possible fields.

Hopefully that can get you started. However, I suspect that you're going to run into some issues with this request. There are two problems here:

  1. I can't find a "gettype" function for JSON. You can query by $type, but it doesn't look like you can actually run a gettype function on a field and have that maps back to the BSON type.
  2. A field can contain data of multiple types, so you'll need a plan to handle this. Even if it's not apparent Mongo could store some numbers as ints and others floats without you really knowing. In fact, with the PHP driver, this is quite possible.

So if you assume that you can solve problem #1, then you should be able to solve problem #2 using a slight variation on "Get all field Names".

It would probably look something like this:

"map" : function() { for (var key in this) { emit(key, [ typeof value[key] ]); } }
"reduce" : function(key, stuff) { return (key, add_to_set(stuff) ); }

So basically you would emit the key and the type of key value (as an array) in the map function. Then from the reduce function you would add unique entries for each type.

At the end of the run you would have data like this

{"_id":[255], "name" : [1,5,8], ... }

Of course, this is all a lot of work, depending on your actual problem, you may just want to ensure (from your code) that you're always putting in the right type of data. Finding the type of data after the data is in the DB is definitely a pain.

好菇凉咱不稀罕他 2024-09-15 09:51:30

利用 styvane 查询,我添加了一个 $group 列表,以便在我们有不同的数据类型时更容易阅读。

db.posts.aggregate( 
[ 
    { "$project": { _id:0, "fieldType": {  "$type": "$date2"  } } },
    {"$group": { _id: {"fieldType": "$fieldType"},count: {$sum: 1}}}
])

并得到这样的结果:

{ "_id" : { "fieldType" : "missing" }, "count" : 50 }
{ "_id" : { "fieldType" : "date" }, "count" : 70 }
{ "_id" : { "fieldType" : "string" }, "count" : 10 }

Taking advantage of the styvane query, I added a $group listing to make it easier to read when we have different data types.

db.posts.aggregate( 
[ 
    { "$project": { _id:0, "fieldType": {  "$type": "$date2"  } } },
    {"$group": { _id: {"fieldType": "$fieldType"},count: {$sum: 1}}}
])

And have this result:

{ "_id" : { "fieldType" : "missing" }, "count" : 50 }
{ "_id" : { "fieldType" : "date" }, "count" : 70 }
{ "_id" : { "fieldType" : "string" }, "count" : 10 }
夕色琉璃 2024-09-15 09:51:30

注意到 a=5;a.constructor.toString() 打印 function Number() { [native code] },可以执行类似以下操作:

db.collection.mapReduce(
function() {
    emit(this._id.constructor.toString()
       .replace(/^function (\S+).+$/,  "$1"), 1);
}, 
function(k, v) {
    return Array.sum(v);
}, 
{ 
    out: { inline: 1 } 
});

Noting that a=5;a.constructor.toString() prints function Number() { [native code] }, one can do something similar to:

db.collection.mapReduce(
function() {
    emit(this._id.constructor.toString()
       .replace(/^function (\S+).+$/,  "$1"), 1);
}, 
function(k, v) {
    return Array.sum(v);
}, 
{ 
    out: { inline: 1 } 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文