Stackoverflow中已经存在一个问题,与我的问题非常相似.问题是这个问题的答案是针对Java驱动程序,我试图在shell中进行.
我这样做......
db.meta.update(
{'fields.properties.default': { $type : 1 }},
{'fields.properties.default': { $type : 2 }}
)
Run Code Online (Sandbox Code Playgroud)
这不行!
我有一组文档,其值已知为数字,但存储为字符串.更改字段的类型是我无法控制的,但我想在聚合中使用该字段(例如,对其进行平均).
似乎我应该在分组之前使用投影,并且在该投影中根据需要转换字段.我似乎无法正确地得到语法 - 我尝试的一切都给了我NaN,或者在聚合的下一步中简单地丢失了新字段.
$project: {
value: '$value',
valueasnumber: ????
}
Run Code Online (Sandbox Code Playgroud)
鉴于上面的一个非常简单的例子,其中所有文档中$ value的内容都是字符串类型,但是将解析为一个数字,我该怎么做使valueasnumber一个新的(不存在的)字段,其类型为double,并且已解析它的价值是多少?
我尝试过类似下面的例子(以及大约十几个类似的东西):
{ $add: new Number('$value').valueOf() }
new Number('$value').valueOf()
Run Code Online (Sandbox Code Playgroud)
我完全吠叫了错误的树吗?任何帮助将不胜感激!
(要100%清楚,下面是我想要使用新字段的方式).
$group {
score: {
$avg: '$valueasnumber'
}
}
Run Code Online (Sandbox Code Playgroud) 我遇到的问题是从字符串转换为整数以创建平均值.我知道在以后的版本中使用$ convert,但我找不到使用$ toInt术语的正确位置.我知道使用此关键字的转换在单个示例的命令行上有效,但我应该将它放在聚合框架中的哪个位置.
db.my_batch.aggregate([{"$unwind": "$current.Data.x"}, {"$match": {"current.Data.x.Typ": "01", "current.Data.x.Value": {"$lt": "TTTT"}}}, {"$project": {"current.Data.x.Value": 1, "uId":1}}, {"$group": {"_id": null, "ad": {"$avg": {"$toInt": "$current.Data.x.Value"}}}} ])
Run Code Online (Sandbox Code Playgroud)
我收到以下回复:
2018-07-20T17:19:42.707+0200 E QUERY [thread1] Error: command failed: {
"ok" : 0,
"errmsg" : "Unrecognized expression '$toInt'",
"code" : 168,
"codeName" : "InvalidPipelineOperator"
} : aggregate failed :
Run Code Online (Sandbox Code Playgroud) 我已将CSV文件导入我的mongodb.CSV具有mongo所需的分隔符,并且通过此查询从MySQL数据库接收:
SELECT * FROM csgo_users INTO OUTFILE 'b1.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
我也尝试使用它的导出功能从Sequel Pro接收CSV.
在我的csv中,我有一个长度为17个字符的字段.在MySQL数据库中,此字段的类型为VARCHAR,但包含17位数字.
将csv导入到mongo之后,我得到了这个类型的字段NumberLong,但我希望它是string.
我到目前为止尝试过:
varchar为text.--headerline和--columnsHaveTypes--fields.试过这样的命令:
db.csgo_users.find({"steam_id": {$type: 18}})
.toArray()
.map(function(v){
v.steam_id = new String(v);
db.csgo_users.save(v)
})
Run Code Online (Sandbox Code Playgroud)或这个:
db.csgo_users.find({"steam_id": {$type: 18}})
.toArray()
.map(function(v){
v.steam_id = new String(v.toSring());
db.csgo_users.save(v)
})
Run Code Online (Sandbox Code Playgroud)
-我已经尝试了很多解决方案与forEach()像这样或这样或本
等.
对于我尝试的最后一个例子,我没有字符串,但是对象,{"N",u","m","b","e","r","L","o","n","g"..}但是我希望它是"123456789",而不是对象.
我正在使用MongoDB 3.4 文档. …
有没有一种方法可以对数字的字符串字段执行小于或等于查询?
示例数据
| Id | Price |
| 1 | "1000.00" |
| 2 | "5400.00" |
| 3 | "750.00" |
Run Code Online (Sandbox Code Playgroud)
当我执行此查询时,它返回所有记录:
db.MyCollection.find({ Price: {$lte: "1000.0"} })
Run Code Online (Sandbox Code Playgroud)
当我执行此查询时,它不返回任何内容:
db.MyCollection.find({ Price: {$lte: 1000.0} })
Run Code Online (Sandbox Code Playgroud) 我有一大堆这样的文件:
{
_id: '1',
colors: [
{ value: 'red', count: 2 },
{ value: 'blue', count: 3}
]
shapes: [
{ value: 'cube', type: '3d' },
{ value: 'square', type: '2d'}
]
},
{
_id: '2',
colors: [
{ value: 'red', count: 7 },
{ value: 'blue', count: 34},
{ value: 'yellow', count: 12}
]
shapes: [
{ value: 'prism', type: '3d' },
{ value: 'triangle', type: '2d'}
]
}
Run Code Online (Sandbox Code Playgroud)
通过使用$unwind和$addToSet,如下所示:
db.getCollection('coll').aggregate([{$unwind:"$colors"},{$unwind:"$shapes"},{$group:{_id:null,colors:{$addToSet:"$colors"},shapes:{$addToSet:"$shapes"}])
Run Code Online (Sandbox Code Playgroud)
我可以得到以下信息:
{
"_id" : …Run Code Online (Sandbox Code Playgroud)