es:
{"_id":1, "a":1},
{"_id":2, "a":3}.. etc..etc
Mongo non prevede l'unset del _id
uno script del genere
db.events.update({},{$set : {_id:new ObjectId()}},{multi:true});
ritorna questo errore:
After applying the update to the document {_id: 4 , ...}, the (immutable) field '_id' was found to have been altered to _id: ObjectId('54801ec49103c90347485eaf')
il modo migliore è quello di prendere tutti i valori ribaltarli su una collection di appoggio (eliminando il vecchio _id) e rinominare la collection in quella sorgente.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use test; | |
var coll = db.events; | |
var coll_new = db.events_new; | |
var cursor = coll.find(); | |
cursor.forEach(function(doc) { | |
delete doc._id; | |
coll_new.save(doc); | |
}); | |
coll_new.renameCollection("events",true); |