Here you can find the source of removeField(BSONObject b, String fieldName)
static Object removeField(BSONObject b, String fieldName)
//package com.java2s; /**/* w ww. ja v a 2s . c om*/ * Copyright (c) 2012, Thilo Planz. All rights reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the Apache License, Version 2.0 * as published by the Apache Software Foundation (the "License"). * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * * You should have received a copy of the License along with this program. * If not, see <http://www.apache.org/licenses/LICENSE-2.0>. */ import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Map; import org.bson.BSON; import org.bson.BSONObject; public class Main { static Object removeField(BSONObject b, String fieldName) { if (fieldName.contains(".")) throw new UnsupportedOperationException("not yet implemented"); return b.removeField(fieldName); } /** * @return true, if the field contains (in case of an array) or is equal to * (in case of a single value) the given Object */ static boolean contains(BSONObject b, String fieldName, Object toLookFor) { Object list = get(b, fieldName); if (list == null) return false; if (list instanceof List<?>) { for (Object o : (List<?>) list) { if (equals(o, toLookFor)) return true; } return false; } if (list instanceof Object[]) { for (Object o : (Object[]) list) { if (equals(o, toLookFor)) return true; } return false; } return (equals(list, toLookFor)); } static Object get(BSONObject b, String fieldName) { if (!fieldName.contains(".")) return notNull(b.get(fieldName)); String[] path = fieldName.split("\\.", 2); Object nested = b.get(path[0]); if (nested == null) return null; if (nested instanceof BSONObject) return get((BSONObject) nested, path[1]); throw new IllegalArgumentException("cannot get field `" + fieldName + "` of " + b); } /** * BSON objects are considered equal when their binary encoding matches */ static boolean equals(BSONObject a, BSONObject b) { return a.keySet().equals(b.keySet()) && Arrays.equals(BSON.encode(a), BSON.encode(b)); } static boolean equals(Object a, Object b) { if (a == b) return true; if (a == null || b == null) return false; if (a instanceof BSONObject) if (b instanceof BSONObject) return equals((BSONObject) a, (BSONObject) b); else return false; if (a instanceof byte[]) { if (b instanceof byte[]) return Arrays.equals((byte[]) a, (byte[]) b); else return false; } return a.equals(b); } private static <T> T notNull(T x) { if (x == null) return x; if (x instanceof Map<?, ?>) { if (((Map<?, ?>) x).isEmpty()) return null; return x; } if (x instanceof Object[]) { if (((Object[]) x).length == 0) return null; return x; } if (x instanceof byte[]) { if (((byte[]) x).length == 0) return null; return x; } if (x instanceof Collection<?>) { if (((Collection<?>) x).isEmpty()) return null; return x; } return x; } }