如何将字符串中的字符转换为整个collection?
How to convert a string with characters in the int for the entire collection?
我有一个 collection 相似的外观:
_id:5d0fe0dcfd8ea94eb4633222
Category:"Stripveiling (Nederlands)"
Category url:"https://www.catawiki.nl/a/11-stripveiling-nederlands"
Lot title:"Erwin Sels (Ersel) - Originele pagina"
Seller name:"Stripwereld"
Seller country:"Nederland"
Bids count:21
Winning bid:"€ 135"
Bid amount:"Closed"
Lot image:"https://assets.catawiki.nl/assets/2011/11/17/7/4/c/74c53540-f390-012e-..."
我需要将 "Winning bid" 字段更改为 int。即,删除货币符号并将整个 collection.
从 string 转换为 int
我在文档中的任何地方都找不到如何做到这一点,我真的必须用 Python 获取每个值,删除货币符号并使用更新方法来做到这一点吗?我有将近800万条记录,会很长。
如何使用 collection 方法执行此操作?或者使用 Python 执行此操作的最快选项是什么?
如果要转换整个集合,可以使用聚合管道来完成。
您需要在 $project
中使用 $substr
和 $toInt(
或 $toDouble
或 $convert
将货币转换为字符串) stage 和 $out
作为聚合的最后阶段。 $out
将聚合管道的结果写入给定的集合名称。
但使用时要小心$out
。根据官方 mongodb 文档:
Create New Collection
The $out
operation creates a new collection in the current database if one does not already exist. The
collection is not visible until the aggregation completes. If the
aggregation fails, MongoDB does not create the collection.
Replace Existing Collection
If the collection specified by the $out
operation already exists, then upon completion of the
aggregation, the $out
stage atomically replaces the existing
collection with the new results collection. Specifically, the $out
operation:
- Creates a temp collection.
- Copies the indexes from the existing
collection to the temp collection.
- Inserts the documents into the
temp collection.
- Calls db.collection.renameCollection with
dropTarget: true to rename the temp collection to the destination
collection.
The $out
operation does not change any indexes that existed on the
previous collection. If the aggregation fails, the $out
operation
makes no changes to the pre-existing collection.
试试这个:
db.collection_name.aggregate([
{
$project: {
category : "$category",
category_name : "$category_name",
lot_title : "$lot_title",
seller_name : "$seller_name",
seller_country : "$seller_country",
bid_count : "$bid_count",
winning_bid : { $toInt : {$substr : ["$winning_bid",2,-1]}},
bid_amount : "$bid_amount",
lot_image : "$lot_image"
}
},{
$out : "collection_name"
}
])
您可能需要使用 allowDiskUse : true
作为聚合管道的选项,因为您有很多文档,它可能超过 16MB mongodb 限制。
不要忘记将 collection_name
替换为实际的集合名称,并在集合中包含您需要的 $project
阶段中的所有必填字段。并且请首先使用不同的 temporary_collection
或仅通过删除 $out 阶段并检查 aggregation
管道的结果来仔细检查该值。
有关详细信息,请阅读官方 mongodb 文档 $out, $toInt, $toDouble, $convert, $substr and allowDiskUse。
我有一个 collection 相似的外观:
_id:5d0fe0dcfd8ea94eb4633222
Category:"Stripveiling (Nederlands)"
Category url:"https://www.catawiki.nl/a/11-stripveiling-nederlands"
Lot title:"Erwin Sels (Ersel) - Originele pagina"
Seller name:"Stripwereld"
Seller country:"Nederland"
Bids count:21
Winning bid:"€ 135"
Bid amount:"Closed"
Lot image:"https://assets.catawiki.nl/assets/2011/11/17/7/4/c/74c53540-f390-012e-..."
我需要将 "Winning bid" 字段更改为 int。即,删除货币符号并将整个 collection.
从 string 转换为 int我在文档中的任何地方都找不到如何做到这一点,我真的必须用 Python 获取每个值,删除货币符号并使用更新方法来做到这一点吗?我有将近800万条记录,会很长。
如何使用 collection 方法执行此操作?或者使用 Python 执行此操作的最快选项是什么?
如果要转换整个集合,可以使用聚合管道来完成。
您需要在 $project
中使用 $substr
和 $toInt(
或 $toDouble
或 $convert
将货币转换为字符串) stage 和 $out
作为聚合的最后阶段。 $out
将聚合管道的结果写入给定的集合名称。
但使用时要小心$out
。根据官方 mongodb 文档:
Create New Collection
The
$out
operation creates a new collection in the current database if one does not already exist. The collection is not visible until the aggregation completes. If the aggregation fails, MongoDB does not create the collection.Replace Existing Collection
If the collection specified by the
$out
operation already exists, then upon completion of the aggregation, the$out
stage atomically replaces the existing collection with the new results collection. Specifically, the$out
operation:
- Creates a temp collection.
- Copies the indexes from the existing collection to the temp collection.
- Inserts the documents into the temp collection.
- Calls db.collection.renameCollection with dropTarget: true to rename the temp collection to the destination collection.
The
$out
operation does not change any indexes that existed on the previous collection. If the aggregation fails, the$out
operation makes no changes to the pre-existing collection.
试试这个:
db.collection_name.aggregate([
{
$project: {
category : "$category",
category_name : "$category_name",
lot_title : "$lot_title",
seller_name : "$seller_name",
seller_country : "$seller_country",
bid_count : "$bid_count",
winning_bid : { $toInt : {$substr : ["$winning_bid",2,-1]}},
bid_amount : "$bid_amount",
lot_image : "$lot_image"
}
},{
$out : "collection_name"
}
])
您可能需要使用 allowDiskUse : true
作为聚合管道的选项,因为您有很多文档,它可能超过 16MB mongodb 限制。
不要忘记将 collection_name
替换为实际的集合名称,并在集合中包含您需要的 $project
阶段中的所有必填字段。并且请首先使用不同的 temporary_collection
或仅通过删除 $out 阶段并检查 aggregation
管道的结果来仔细检查该值。
有关详细信息,请阅读官方 mongodb 文档 $out, $toInt, $toDouble, $convert, $substr and allowDiskUse。