All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anwen.mongo.bson.MongoPlusBasicDBObject Maven / Gradle / Ivy

The newest version!
package com.anwen.mongo.bson;

import com.anwen.mongo.cache.codec.MapCodecCache;
import com.anwen.mongo.support.SFunction;
import com.mongodb.BasicDBObject;
import org.bson.BsonDocument;
import org.bson.conversions.Bson;

/**
 * 支持lambda的BasicDBObject
 * @author JiaChaoYang
 * @date 2023-11-14 15:01
 **/
public class MongoPlusBasicDBObject extends BasicDBObject {

    public  void put(SFunction key,BasicDBObject value){
        put(key.getFieldNameLine(),value);
    }

    public  void append(SFunction key,BasicDBObject value){
        super.append(key.getFieldNameLine(),value);
    }

    public  void get(SFunction key){
        super.get(key.getFieldNameLine());
    }

    public  void putIsNotNull(String key, Object value){
        if (value != null) {
            super.put(key, value);
        }
    }

    public  void put(SFunction key, SFunction value){
        super.put(key.getFieldNameLine(),value.getFieldNameLine());
    }

    public  void putOption(SFunction key, SFunction value){
        super.put(key.getFieldNameLine(),value.getFieldNameLineOption());
    }

    public  boolean containsKey(SFunction key){
        return super.containsKey(key.getFieldNameLine());
    }

    public  Object remove(SFunction key){
        return super.remove(key.getFieldNameLine());
    }

    public void put(String key,BasicDBObject value){
        if (containsKey(key)){
            super.put(key,new BasicDBObject((BasicDBObject) get(key)){{
                value.keySet().forEach(basic -> {
                    append(basic,value.get(basic));
                });
            }});
        }else {
            super.put(key,value);
        }
    }

    public void put(Bson bson){
        BsonDocument superBsonDocument = super.toBsonDocument(BsonDocument.class, MapCodecCache.getDefaultCodecRegistry());
        BsonDocument bsonDocument = bson.toBsonDocument(BsonDocument.class, MapCodecCache.getDefaultCodecRegistry());
        bsonDocument.forEach((k,v) -> {
            if (super.containsKey(k)){
                Object value = get(k);
                if (value instanceof BsonDocument){
                    ((BsonDocument) value).putAll(v.asDocument());
                }
            }else {
                super.putAll(bsonDocument);
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy