脚本中的 parseInt() 返回 NaN 因为 jsonpath 语法似乎没有被正确读取
parseInt() in script returning NaN because jsonpath syntax seems to not be read correctly
使用 jsonpath 点符号,我需要的关键是在-
ProductDetails.Prices.BillingDivisor
示例 JSON 文档如下所示...
{
"UniqueID": "50962",
"InvNum": "17001107",
"Location": "017",
"InvoiceDate": "6/19/2017 12:00:00 AM",
"LINEITEM": "1",
"CUSTID": "011022",
"DEPTID": "004305",
"PRODID": "070023",
"QUANT": "0",
"Unitprice": 1.887,
"BILLUNIT": "GL",
"QuantShare": 2000,
"Tax": "False",
"TaxExempt": "",
"PriceType": "List",
"DumItem": "False",
"SourceNumber": "0",
"SourceLocation": "",
"SourceDate": "1/1/1900 12:00:00 AM",
"LastUpdated": "2018-05-03T18:49:00-0400",
"BookGuid": null,
"ProductDetails": {
"UniqueKey": 98269,
"DepartmentID": "004305",
"ProductID": "070023",
"Accounts": {
"DepartmentID": "004305",
"ProductID": "070023",
"SalesTaxable": false,
"SalesAccount": "",
"BeginningYearBalance": 0,
"EndOfMonth1": 0,
"EndOfMonth2": 0,
"EndOfMonth3": 0,
"EndOfMonth4": 0,
"EndOfMonth5": 0,
"EndOfMonth6": 0,
"EndOfMonth7": 0,
"EndOfMonth8": 0,
"EndOfMonth9": 0,
"EndOfMonth10": 0,
"EndOfMonth11": 0,
"EndOfMonth12": 0,
"ProductStateTaxable": false,
"PurchaseAccount": "",
"FreightAccount": "",
"PurchaseDiscount": ""
},
"Prices": {
"DepartmentID": "004305",
"ProductID": "070023",
"BillingUnits": "GL",
"BillingDivisor": "1",
"AverageCost": "2.129",
"List": "0",
"LastPurchaseCost": "2.129"
},
"Classifications": {
"Name-0": "5314: Dieselex",
"Name-1": "B11 DYED: B11 DYED",
"Name-2": "Gallon Products",
"Name-3": "FUEL-CvilleDYED",
"Name-4": "Energy Comm"
},
"Attributes": {
"Value-0": "",
"Name-0": "Bag Weight",
"Required-0": false,
"MaximumLength-0": 0,
"DisallowDuplicates-0": false,
"Value-1": "",
"Name-1": "GTIN",
"Required-1": false,
"MaximumLength-1": 0,
"DisallowDuplicates-1": false
},
"LastUpdated": "2018-05-03T18:19:18-0400",
"ProductClassification": "5314: Dieselex"
}
}
使用以下 javascript,除了这部分之外的所有值 return 都是正确的:
parseInt(data.ProductDetails.Prices.BillingDivisor)
因为我一直得到 NaN 而不是 1 的结果。
var bulk = db.bookingsMaster.initializeOrderedBulkOp();
var counter = 0;
db.bookingsMaster.find().forEach(function (data) {
var updoc = {
"$set": {
}
};
var unitPrice = "Unitprice";
updoc[ "$set"][unitPrice] = parseFloat(data.Unitprice);
var quantShare = "QuantShare";
updoc[ "$set"][quantShare] = parseFloat(data.QuantShare);
var billingDivisor = "ProductDetails.Prices.BillingDivisor";
updoc[ "$set"][billingDivisor] = parseInt(data.ProductDetails.Prices.BillingDivisor);
// 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.bookingsMaster.initializeOrderedBulkOp();
}
})
// Add the rest in the queue
if (counter % 1000 != 0) bulk.execute();
非常感谢任何帮助。
你的问题是,与 Unitprice 和 QuantShare 不同,BillingDivisor 实际上是一个在 1 周围加上双引号的字符串。如果你尝试
console.log(parseInt('"1"'))
如您所见,您将获得 NaN
。
您需要去掉值中的双引号,例如
console.log(parseInt('"1"'.replace('"','')))
其中returns 1,随心所欲。所以在你的代码中,你需要
parseInt(data.ProductDetails.Prices.BillingDivisor.replace('"',''))
使用 jsonpath 点符号,我需要的关键是在-
ProductDetails.Prices.BillingDivisor
示例 JSON 文档如下所示...
{
"UniqueID": "50962",
"InvNum": "17001107",
"Location": "017",
"InvoiceDate": "6/19/2017 12:00:00 AM",
"LINEITEM": "1",
"CUSTID": "011022",
"DEPTID": "004305",
"PRODID": "070023",
"QUANT": "0",
"Unitprice": 1.887,
"BILLUNIT": "GL",
"QuantShare": 2000,
"Tax": "False",
"TaxExempt": "",
"PriceType": "List",
"DumItem": "False",
"SourceNumber": "0",
"SourceLocation": "",
"SourceDate": "1/1/1900 12:00:00 AM",
"LastUpdated": "2018-05-03T18:49:00-0400",
"BookGuid": null,
"ProductDetails": {
"UniqueKey": 98269,
"DepartmentID": "004305",
"ProductID": "070023",
"Accounts": {
"DepartmentID": "004305",
"ProductID": "070023",
"SalesTaxable": false,
"SalesAccount": "",
"BeginningYearBalance": 0,
"EndOfMonth1": 0,
"EndOfMonth2": 0,
"EndOfMonth3": 0,
"EndOfMonth4": 0,
"EndOfMonth5": 0,
"EndOfMonth6": 0,
"EndOfMonth7": 0,
"EndOfMonth8": 0,
"EndOfMonth9": 0,
"EndOfMonth10": 0,
"EndOfMonth11": 0,
"EndOfMonth12": 0,
"ProductStateTaxable": false,
"PurchaseAccount": "",
"FreightAccount": "",
"PurchaseDiscount": ""
},
"Prices": {
"DepartmentID": "004305",
"ProductID": "070023",
"BillingUnits": "GL",
"BillingDivisor": "1",
"AverageCost": "2.129",
"List": "0",
"LastPurchaseCost": "2.129"
},
"Classifications": {
"Name-0": "5314: Dieselex",
"Name-1": "B11 DYED: B11 DYED",
"Name-2": "Gallon Products",
"Name-3": "FUEL-CvilleDYED",
"Name-4": "Energy Comm"
},
"Attributes": {
"Value-0": "",
"Name-0": "Bag Weight",
"Required-0": false,
"MaximumLength-0": 0,
"DisallowDuplicates-0": false,
"Value-1": "",
"Name-1": "GTIN",
"Required-1": false,
"MaximumLength-1": 0,
"DisallowDuplicates-1": false
},
"LastUpdated": "2018-05-03T18:19:18-0400",
"ProductClassification": "5314: Dieselex"
}
}
使用以下 javascript,除了这部分之外的所有值 return 都是正确的:
parseInt(data.ProductDetails.Prices.BillingDivisor)
因为我一直得到 NaN 而不是 1 的结果。
var bulk = db.bookingsMaster.initializeOrderedBulkOp();
var counter = 0;
db.bookingsMaster.find().forEach(function (data) {
var updoc = {
"$set": {
}
};
var unitPrice = "Unitprice";
updoc[ "$set"][unitPrice] = parseFloat(data.Unitprice);
var quantShare = "QuantShare";
updoc[ "$set"][quantShare] = parseFloat(data.QuantShare);
var billingDivisor = "ProductDetails.Prices.BillingDivisor";
updoc[ "$set"][billingDivisor] = parseInt(data.ProductDetails.Prices.BillingDivisor);
// 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.bookingsMaster.initializeOrderedBulkOp();
}
})
// Add the rest in the queue
if (counter % 1000 != 0) bulk.execute();
非常感谢任何帮助。
你的问题是,与 Unitprice 和 QuantShare 不同,BillingDivisor 实际上是一个在 1 周围加上双引号的字符串。如果你尝试
console.log(parseInt('"1"'))
如您所见,您将获得 NaN
。
您需要去掉值中的双引号,例如
console.log(parseInt('"1"'.replace('"','')))
其中returns 1,随心所欲。所以在你的代码中,你需要
parseInt(data.ProductDetails.Prices.BillingDivisor.replace('"',''))