返回 MongoDB 中字段的实际类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3208538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:45:31  来源:igfitidea点击:

Return actual type of a field in MongoDB

mongodbmongodb-queryaggregation-framework

提问by nikcub

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

在 MongoDB 中,使用$type,可以根据字段是否与 BSON 数据类型匹配来过滤搜索(请参阅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?

我需要一个查询来告诉我该字段的实际类型是什么,对于集合中的每个字段。这对 MongoDB 有可能吗?

采纳答案by Gates VP

OK, here are some related questions that may help:

好的,这里有一些相关的问题可能会有所帮助:

Get all field namesin a collection using map-reduce.

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

Here's a recursive versionthat 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 gettypefunction 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.
  1. 我找不到 JSON 的“gettype”函数。您可以查询 by $type,但看起来您实际上无法gettype在字段上运行函数并将该函数映射回 BSON 类型。
  2. 一个字段可以包含多种类型的数据,因此您需要一个计划来处理这个问题。即使不是很明显,Mongo 也可以将一些数字存储为整数,而其他数字则在您不知道的情况下存储。事实上,有了PHP驱动,这是完全有可能的。

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".

因此,如果您假设您可以解决问题 #1,那么您应该能够使用"Get all field Names"的细微变化来解决问题 #2 。

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 keyand 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.

所以基本上你会在 map 函数中发出 thekey和 the type of key value(作为一个数组)。然后从 reduce 函数中,您将为每种类型添加唯一的条目。

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

在运行结束时,您将拥有这样的数据

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

{"_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.

当然,这需要大量工作,具体取决于您的实际问题,您可能只想确保(从您的代码中)始终输入正确类型的数据。数据进了DB之后再找数据类型,绝对是一件痛苦的事情。

回答by styvane

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

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

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

which yields:

产生:

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

回答by Bharathiraja

type the below query in mongo shell

在 mongo shell 中输入以下查询

  typeof db.employee.findOne().first_name

Syntax

句法

 typeof db.collection_name.findOne().field_name

回答by ChrisF

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

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

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