Naf*_*sen 36 mongodb mongodb-query
我试图将包含数值的字符串转换为MongoDB中的聚合查询中的值.
文件示例
{
"_id": ObjectId("5522XXXXXXXXXXXX"),
"Date": "2015-04-05",
"PartnerID": "123456",
"moop": "1234"
}
Run Code Online (Sandbox Code Playgroud)
我使用的聚合查询的示例
{
aggregate: 'my_collection',
pipeline: [
{$match: {
Date :
{$gt:'2015-04-01',
$lt: '2015-04-05'
}}
},
{$group:
{_id: "$PartnerID",
total:{$sum:'$moop'}
}}]}
Run Code Online (Sandbox Code Playgroud)
结果在哪里
{
"result": [
{
"_id": "123456",
"total": NumberInt(0)
}
}
Run Code Online (Sandbox Code Playgroud)
如何将字符串转换为其数值?
Yog*_*esh 28
MongoDB聚合不允许更改给定字段的现有数据类型.在这种情况下,您应该创建一些转换string为的编程代码int.检查以下代码
db.collectionName.find().forEach(function(data) {
db.collectionName.update({
"_id": data._id,
"moop": data.moop
}, {
"$set": {
"PartnerID": parseInt(data.PartnerID)
}
});
})
Run Code Online (Sandbox Code Playgroud)
如果您的集合大小超过以上脚本会降低性能,对于perfomace mongo提供mongo批量操作,使用mongo批量操作也更新数据类型
var bulk = db.collectionName.initializeOrderedBulkOp();
var counter = 0;
db.collectionName.find().forEach(function(data) {
var updoc = {
"$set": {}
};
var myKey = "PartnerID";
updoc["$set"][myKey] = parseInt(data.PartnerID);
// queue the update
bulk.find({
"_id": data._id
}).update(updoc);
counter++;
// Drain and re-initialize every 1000 update statements
if (counter % 1000 == 0) {
bulk.execute();
bulk = db.collectionName.initializeOrderedBulkOp();
}
})
// Add the rest in the queue
if (counter % 1000 != 0) bulk.execute();
Run Code Online (Sandbox Code Playgroud)
这基本上减少了发送到服务器的操作语句数量,每1000个排队操作只发送一次.
小智 16
您可以轻松地将字符串数据类型转换为数字数据类型.
不要忘记更改collectionName和FieldName.例如:CollectionNmae:Users&FieldName:Contactno.
试试这个查询..
db.collectionName.find().forEach( function (x) {
x.FieldName = parseInt(x.FieldName);
db.collectionName.save(x);
});
Run Code Online (Sandbox Code Playgroud)
Naf*_*sen 12
最终我用了
db.my_collection.find({moop: {$exists: true}}).forEach(function(obj) {
obj.moop = new NumberInt(obj.moop);
db.my_collection.save(obj);
});
Run Code Online (Sandbox Code Playgroud)
把moop从字符串中my_collection整数按照西蒙的回答的例子MongoDB的:如何改变一个字段的类型?.
chr*_*dam 11
使用MongoDB 4.0和更新版本
你有两个选择,即$toInt或$convert.使用$toInt,请按照以下示例:
filterDateStage = {
'$match': {
'Date': {
'$gt': '2015-04-01',
'$lt': '2015-04-05'
}
}
};
groupStage = {
'$group': {
'_id': '$PartnerID',
'total': { '$sum': { '$toInt': '$moop' } }
}
};
db.getCollection('my_collection').aggregate([
filterDateStage,
groupStage
])
Run Code Online (Sandbox Code Playgroud)
如果转换操作遇到错误,则聚合操作将停止并引发错误.要覆盖此行为,请$convert改用.
运用 $convert
groupStage = {
'$group': {
'_id': '$PartnerID',
'total': {
'$sum': {
'$convert': { 'input': '$moop', 'to': 'int' }
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
使用Map/Reduce
使用map/reduce,您可以使用javascript函数parseInt()来进行转换.例如,您可以定义map函数来处理每个输入文档:在函数中,this指的是map-reduce操作正在处理的文档.该函数将转换后的moop字符串值映射到PartnerID每个文档,并发出PartnerID转换后的moop对.这是parseInt()可以应用javascript本机函数的地方:
var mapper = function () {
var x = parseInt(this.moop);
emit(this.PartnerID, x);
};
Run Code Online (Sandbox Code Playgroud)
接着,两个参数定义了相应的减少功能keyCustId和valuesMoop.valuesMoop是一个数组,其元素是moopmap函数发出的整数值并按其分组keyPartnerID.该函数将valuesMoop数组减少为其元素的总和.
var reducer = function(keyPartnerID, valuesMoop) {
return Array.sum(valuesMoop);
};
db.collection.mapReduce(
mapper,
reducer,
{
out : "example_results",
query: {
Date: {
$gt: "2015-04-01",
$lt: "2015-04-05"
}
}
}
);
db.example_results.find(function (err, docs) {
if(err) console.log(err);
console.log(JSON.stringify(docs));
});
Run Code Online (Sandbox Code Playgroud)
例如,使用以下示例文档集合:
/* 0 */
{
"_id" : ObjectId("550c00f81bcc15211016699b"),
"Date" : "2015-04-04",
"PartnerID" : "123456",
"moop" : "1234"
}
/* 1 */
{
"_id" : ObjectId("550c00f81bcc15211016699c"),
"Date" : "2015-04-03",
"PartnerID" : "123456",
"moop" : "24"
}
/* 2 */
{
"_id" : ObjectId("550c00f81bcc15211016699d"),
"Date" : "2015-04-02",
"PartnerID" : "123457",
"moop" : "21"
}
/* 3 */
{
"_id" : ObjectId("550c00f81bcc15211016699e"),
"Date" : "2015-04-02",
"PartnerID" : "123457",
"moop" : "8"
}
Run Code Online (Sandbox Code Playgroud)
上面的Map/Reduce操作会将结果保存到example_results集合中,shell命令db.example_results.find()将给出:
/* 0 */
{
"_id" : "123456",
"value" : 1258
}
/* 1 */
{
"_id" : "123457",
"value" : 29
}
Run Code Online (Sandbox Code Playgroud)
这是针对这个问题的纯基于 MongoDB 的解决方案,我只是为了好玩而写的。它实际上是一个服务器端字符串到数字的解析器,支持正数和负数以及小数:
db.collection.aggregate({
$addFields: {
"moop": {
$reduce: {
"input": {
$map: { // split string into char array so we can loop over individual characters
"input": {
$range: [ 0, { $strLenCP: "$moop" } ] // using an array of all numbers from 0 to the length of the string
},
"in":{
$substrCP: [ "$moop", "$$this", 1 ] // return the nth character as the mapped value for the current index
}
}
},
"initialValue": { // initialize the parser with a 0 value
"n": 0, // the current number
"sign": 1, // used for positive/negative numbers
"div": null, // used for shifting on the right side of the decimal separator "."
"mult": 10 // used for shifting on the left side of the decimal separator "."
}, // start with a zero
"in": {
$let: {
"vars": {
"n": {
$switch: { // char-to-number mapping
branches: [
{ "case": { $eq: [ "$$this", "1" ] }, "then": 1 },
{ "case": { $eq: [ "$$this", "2" ] }, "then": 2 },
{ "case": { $eq: [ "$$this", "3" ] }, "then": 3 },
{ "case": { $eq: [ "$$this", "4" ] }, "then": 4 },
{ "case": { $eq: [ "$$this", "5" ] }, "then": 5 },
{ "case": { $eq: [ "$$this", "6" ] }, "then": 6 },
{ "case": { $eq: [ "$$this", "7" ] }, "then": 7 },
{ "case": { $eq: [ "$$this", "8" ] }, "then": 8 },
{ "case": { $eq: [ "$$this", "9" ] }, "then": 9 },
{ "case": { $eq: [ "$$this", "0" ] }, "then": 0 },
{ "case": { $and: [ { $eq: [ "$$this", "-" ] }, { $eq: [ "$$value.n", 0 ] } ] }, "then": "-" }, // we allow a minus sign at the start
{ "case": { $eq: [ "$$this", "." ] }, "then": "." }
],
default: null // marker to skip the current character
}
}
},
"in": {
$switch: {
"branches": [
{
"case": { $eq: [ "$$n", "-" ] },
"then": { // handle negative numbers
"sign": -1, // set sign to -1, the rest stays untouched
"n": "$$value.n",
"div": "$$value.div",
"mult": "$$value.mult",
},
},
{
"case": { $eq: [ "$$n", null ] }, // null is the "ignore this character" marker
"then": "$$value" // no change to current value
},
{
"case": { $eq: [ "$$n", "." ] },
"then": { // handle decimals
"n": "$$value.n",
"sign": "$$value.sign",
"div": 10, // from the decimal separator "." onwards, we start dividing new numbers by some divisor which starts at 10 initially
"mult": 1, // and we stop multiplying the current value by ten
},
},
],
"default": {
"n": {
$add: [
{ $multiply: [ "$$value.n", "$$value.mult" ] }, // multiply the already parsed number by 10 because we're moving one step to the right or by one once we're hitting the decimals section
{ $divide: [ "$$n", { $ifNull: [ "$$value.div", 1 ] } ] } // add the respective numerical value of what we look at currently, potentially divided by a divisor
]
},
"sign": "$$value.sign",
"div": { $multiply: [ "$$value.div" , 10 ] },
"mult": "$$value.mult"
}
}
}
}
}
}
}
}
}, {
$addFields: { // fix sign
"moop": { $multiply: [ "$moop.n", "$moop.sign" ] }
}
})
Run Code Online (Sandbox Code Playgroud)
我当然不会把它宣传为蜜蜂的膝盖或任何东西,它可能会对基于客户端的解决方案的更大数据集产生严重的性能影响,但在某些情况下它可能会派上用场......
上述管道将转换以下文件:
{ "moop": "12345" } --> { "moop": 12345 }
Run Code Online (Sandbox Code Playgroud)
和
{ "moop": "123.45" } --> { "moop": 123.45 }
Run Code Online (Sandbox Code Playgroud)
和
{ "moop": "-123.45" } --> { "moop": -123.45 }
Run Code Online (Sandbox Code Playgroud)
和
{ "moop": "2018-01-03" } --> { "moop": 20180103.0 }
Run Code Online (Sandbox Code Playgroud)
可以使用$ toInt运算符在MongoDB v4.0 中将字符串转换为数字。在这种情况下
db.col.aggregate([
{
$project: {
_id: 0,
moopNumber: { $toInt: "$moop" }
}
}
])
Run Code Online (Sandbox Code Playgroud)
输出:
{ "moopNumber" : 1234 }
Run Code Online (Sandbox Code Playgroud)
小智 5
需要注意三件事:
| 归档时间: |
|
| 查看次数: |
75194 次 |
| 最近记录: |