Discussion:
how to remove a field from all documents using Java
ajay
2012-08-23 21:07:22 UTC
Permalink
One of the field in a collection has become obsolete and I want to remove
it from all documents using Java language. How do I go about doing this?

Thanks.
matts
2012-08-23 21:29:04 UTC
Permalink
Mongo().getDB("test").getCollection("bar").update( new BasicDBObject() ,
new BasicDBObject( "$unset" , new BasicDBObject( "badField" : 1 ) ) , false
, true );
Post by ajay
One of the field in a collection has become obsolete and I want to remove
it from all documents using Java language. How do I go about doing this?
Thanks.
-Ajay
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
ajay
2012-08-27 16:33:04 UTC
Permalink
Thanks. Is there a way to do this using Morphia?
Post by matts
Mongo().getDB("test").getCollection("bar").update( new BasicDBObject() ,
new BasicDBObject( "$unset" , new BasicDBObject( "badField" : 1 ) ) , false
, true );
Post by ajay
One of the field in a collection has become obsolete and I want to remove
it from all documents using Java language. How do I go about doing this?
Thanks.
-Ajay
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
ajay
2012-08-27 17:20:18 UTC
Permalink
I figured out how to do this in Morphia. Let me share with others. I
created a generic method for myself. Notice since the field being removed
may no longer be part of the POJO, I am disabling validation both for query
as well as for update-operations.

public <T> void removeField(Class<T> clazz, String fieldName) {
UpdateOperations<T> ops = ds.createUpdateOperations(clazz)
.disableValidation().unset(fieldName);
Query<T> query =
ds.createQuery(clazz).disableValidation().field(fieldName).exists();
UpdateResults<T> updateResults = ds.update(query, ops, false);
}


-Ajay
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
Continue reading on narkive:
Loading...