답변:
$type데이터 를 변경하는 유일한 방법 은 데이터의 유형이 올바른 데이터를 업데이트하는 것입니다.
이 경우 $type 1 (double)에서 2 (string) 로 변경하려고하는 것 같습니다 .
따라서 DB에서 문서를로드하고 캐스트 ( new String(x))를 수행 한 다음 문서를 다시 저장하십시오.
프로그래밍 방식으로 완전히 쉘에서이 작업을 수행해야하는 경우 find(...).forEach(function(x) {})구문을 사용할 수 있습니다 .
아래 두 번째 의견에 대한 답변. 필드 bad를 collection의 숫자에서 문자열로 변경하십시오 foo.
db.foo.find( { 'bad' : { $type : 1 } } ).forEach( function (x) {
x.bad = new String(x.bad); // convert field to string
db.foo.save(x);
});
new String(x.bad)0-index-item x.bad값을 가진 Strings 컬렉션을 만듭니다 . ""+x.badSimone에 의해 설명 된 변형 은 원하는대로 작동합니다
db.questions.find({_id:{$type:16}}).forEach( function (x) { db.questions.remove({_id:x._id},true); x._id = ""+x._id; db.questions.save(x); });
문자열 필드를 정수로 변환하십시오.
db.db-name.find({field-name: {$exists: true}}).forEach(function(obj) {
obj.field-name = new NumberInt(obj.field-name);
db.db-name.save(obj);
});
정수 필드를 문자열로 변환 :
db.db-name.find({field-name: {$exists: true}}).forEach(function(obj) {
obj.field-name = "" + obj.field-name;
db.db-name.save(obj);
});
NumberLong과 같이 사용 하십시오.db.db-name.find({field-name : {$exists : true}}).forEach( function(obj) { obj.field-name = new NumberLong(obj.field-name); db.db-name.save(obj); } );
문자열을 int로 변환합니다.
db.my_collection.find().forEach( function(obj) {
obj.my_value= new NumberInt(obj.my_value);
db.my_collection.save(obj);
});
문자열을 이중으로 변환합니다.
obj.my_value= parseInt(obj.my_value, 10);
플로트의 경우 :
obj.my_value= parseFloat(obj.my_value);
radix- developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/...
new NumberInt()
시작 Mongo 4.2, db.collection.update()마침내 자신의 값에 따라 필드의 업데이트를 허용 집계 파이프 라인을 받아 들일 수 있습니다 :
// { a: "45", b: "x" }
// { a: 53, b: "y" }
db.collection.update(
{ a : { $type: 1 } },
[{ $set: { a: { $toString: "$a" } } }],
{ multi: true }
)
// { a: "45", b: "x" }
// { a: "53", b: "y" }
첫 번째 부분 { a : { $type: 1 } }은 일치 쿼리입니다.
"a"값이 double 일 때 문자열 로 변환하려고하므로 (double) "a"유형의 요소와 일치합니다 1.두 번째 부분 [{ $set: { a: { $toString: "$a" } } }]은 업데이트 집계 파이프 라인입니다.
$setMongo 4.2이 경우 필드를 수정 하는 새로운 집계 연산자 ( )입니다."$set"의 값 "a"에 "$a"변환 "$toString".Mongo 4.2은 문서를 업데이트 할 때 문서 자체를 참조 할 수 있다는 것입니다.의 새로운 값 "a"은의 기존 값을 기반으로합니다 "$a"."$toString"도입 된 새로운 집계 연산자는 무엇입니까 Mongo 4.0?잊지 마십시오 { multi: true }. 그렇지 않으면 첫 번째 일치하는 문서 만 업데이트됩니다.
경우 캐스트가 아닌 이중에서 문자열로, 당신이 소개 다른 변환 사업자 사이의 선택이 Mongo 4.0등을 $toBool, $toInt...
대상 유형에 맞는 전용 변환기가없는 경우 다음 표 에서 값을 찾을 수 { $toString: "$a" }있는 $convert작업으로 대체 할 수 있습니다 .{ $convert: { input: "$a", to: 2 } }to
db.collection.update(
{ a : { $type: 1 } },
[{ $set: { a: { $convert: { input: "$a", to: 2 } } } }],
{ multi: true }
)
db.collection.updateMany( { a : { $type: 1 } }, [{ $set: { a: { $toString: "$a" } } }] )-를 multi : true사용하여 피할 수 있습니다updateMany
모든 대답은 지금까지 클라이언트 측의 모든 컬렉션 요소를 반복하여 일부 버전의 forEach를 사용합니다.
그러나 집계 파이프 라인 및 $ out 스테이지 를 다음과 같이 사용하여 MongoDB의 서버 측 처리를 사용할 수 있습니다 .
$ out 스테이지는 기존 콜렉션을 새 결과 콜렉션으로 원자 적으로 대체합니다.
예:
db.documents.aggregate([
{
$project: {
_id: 1,
numberField: { $substr: ['$numberField', 0, -1] },
otherField: 1,
differentField: 1,
anotherfield: 1,
needolistAllFieldsHere: 1
},
},
{
$out: 'documents',
},
]);
문자열 유형의 필드를 날짜 필드로 변환하려면 find()메소드를 사용하여 forEach()메소드가 리턴 한 커서를 반복 하고 루프 내에서 필드를 Date 오브젝트로 변환 한 다음 $set연산자를 사용하여 필드를 업데이트해야합니다 .
대량 업데이트를 위해 대량 API 를 사용하는 경우 1000 개 단위로 작업을 서버에 보낼 때 더 나은 성능을 제공합니다. 이렇게하면 모든 요청을 서버에 한 번에 하나씩 보내지 않아도 성능이 향상됩니다. 요청 1,000 개
다음은이 접근법을 보여줍니다. 첫 번째 예는 MongoDB 버전에서 사용 가능한 벌크 API를 사용합니다 >= 2.6 and < 3.2. 모든 created_at필드를 날짜 필드 로 변경하여 콜렉션의 모든 문서를 업데이트 합니다.
var bulk = db.collection.initializeUnorderedBulkOp(),
counter = 0;
db.collection.find({"created_at": {"$exists": true, "$type": 2 }}).forEach(function (doc) {
var newDate = new Date(doc.created_at);
bulk.find({ "_id": doc._id }).updateOne({
"$set": { "created_at": newDate}
});
counter++;
if (counter % 1000 == 0) {
bulk.execute(); // Execute per 1000 operations and re-initialize every 1000 update statements
bulk = db.collection.initializeUnorderedBulkOp();
}
})
// Clean up remaining operations in queue
if (counter % 1000 != 0) { bulk.execute(); }
다음 예제는 새로운 MongoDB의 버전에 적용 3.2이후 한 대량의 API를 사용되지 않는 사용하는 API의 새로운 세트 및 제공 bulkWrite():
var bulkOps = [];
db.collection.find({"created_at": {"$exists": true, "$type": 2 }}).forEach(function (doc) {
var newDate = new Date(doc.created_at);
bulkOps.push(
{
"updateOne": {
"filter": { "_id": doc._id } ,
"update": { "$set": { "created_at": newDate } }
}
}
);
})
db.collection.bulkWrite(bulkOps, { "ordered": true });
컬렉션에서 여러 필드의 데이터 형식을 변경해야하므로 다음을 사용하여 문서 컬렉션에서 여러 데이터 형식을 변경했습니다. 오래된 질문에 대한 답변이지만 다른 사람에게는 도움이 될 수 있습니다.
db.mycoll.find().forEach(function(obj) {
if (obj.hasOwnProperty('phone')) {
obj.phone = "" + obj.phone; // int or longint to string
}
if (obj.hasOwnProperty('field-name')) {
obj.field-name = new NumberInt(obj.field-name); //string to integer
}
if (obj.hasOwnProperty('cdate')) {
obj.cdate = new ISODate(obj.cdate); //string to Date
}
db.mycoll.save(obj);
});
You can easily convert the string data type to numerical data type.
Don't forget to change collectionName & FieldName.
for ex : CollectionNmae : Users & FieldName : Contactno.
이 질문을 시도하십시오.
db.collectionName.find().forEach( function (x) {
x.FieldName = parseInt(x.FieldName);
db.collectionName.save(x);
});
mongoose를 사용하여 문자열에서 mongo objectId로 필드 중간 데모 변경 유형
Post.find({}, {mid: 1,_id:1}).exec(function (err, doc) {
doc.map((item, key) => {
Post.findByIdAndUpdate({_id:item._id},{$set:{mid: mongoose.Types.ObjectId(item.mid)}}).exec((err,res)=>{
if(err) throw err;
reply(res);
});
});
});
Mongo ObjectId는 다음과 같은 스타일의 또 다른 예입니다.
답이 다른 사람에게 도움이되기를 희망하는 숫자, 문자열, 부울.
mongodb 콘솔 에서이 스크립트를 문자열에서 부동 변환으로 사용합니다 ...
db.documents.find({ 'fwtweaeeba' : {$exists : true}}).forEach( function(obj) {
obj.fwtweaeeba = parseFloat( obj.fwtweaeeba );
db.documents.save(obj); } );
db.documents.find({ 'versions.0.content.fwtweaeeba' : {$exists : true}}).forEach( function(obj) {
obj.versions[0].content.fwtweaeeba = parseFloat( obj.versions[0].content.fwtweaeeba );
db.documents.save(obj); } );
db.documents.find({ 'versions.1.content.fwtweaeeba' : {$exists : true}}).forEach( function(obj) {
obj.versions[1].content.fwtweaeeba = parseFloat( obj.versions[1].content.fwtweaeeba );
db.documents.save(obj); } );
db.documents.find({ 'versions.2.content.fwtweaeeba' : {$exists : true}}).forEach( function(obj) {
obj.versions[2].content.fwtweaeeba = parseFloat( obj.versions[2].content.fwtweaeeba );
db.documents.save(obj); } );
그리고 이것은 PHP에서))))
foreach($db->documents->find(array("type" => "chair")) as $document){
$db->documents->update(
array('_id' => $document[_id]),
array(
'$set' => array(
'versions.0.content.axdducvoxb' => (float)$document['versions'][0]['content']['axdducvoxb'],
'versions.1.content.axdducvoxb' => (float)$document['versions'][1]['content']['axdducvoxb'],
'versions.2.content.axdducvoxb' => (float)$document['versions'][2]['content']['axdducvoxb'],
'axdducvoxb' => (float)$document['axdducvoxb']
)
),
array('$multi' => true)
);
}
내 경우에는 다음을 사용합니다.
function updateToSting(){
var collection = "<COLLECTION-NAME>";
db.collection(collection).find().forEach(function(obj) {
db.collection(collection).updateOne({YOUR_CONDITIONAL_FIELD:obj.YOUR_CONDITIONAL_FIELD},{$set:{YOUR_FIELD:""+obj.YOUR_FIELD}});
});
}
toString있고 문서의 일부를 가지고 있다면 내가 만든 작은 프로그램이 있습니다.