sql >> データベース >  >> NoSQL >> MongoDB

日付範囲のMongodbJavaクエリ

    ユースケース用のMongoDB(v3.4より前)のシェルコマンドは次のとおりです。

    db.collection.aggregate([
        {
            "$redact": {
                "$cond": [
                    { "$gt": [ "$pub-date", "$rel-date" ] },
                    "$$KEEP",
                    "$$PRUNE"
                ]
            }
        }
    ])
    

    このコマンドをJavaに変換すると、次のようになります。

    MongoClient mongoClient = ...;
    
    MongoCollection<Document> collection = mongoClient.getDatabase("...").getCollection("...");
    
    List<Document> documents = collection.aggregate(Arrays.asList(
            new Document("$redact", new Document("$cond",
                    Arrays.asList(new Document("$gt", Arrays.asList("$pub-date", "$rel-date")), "$$KEEP", "$$PRUNE"))
            ))).into(new ArrayList<>());
    
    for (Document document : documents) {
        System.out.println(document.toJson());
    }
    

    これらのドキュメントを含むコレクションが与えられた場合...

    {
        "_id" : ObjectId("5acb40d27d63b61cb002bafe"),
        "title" : "WingsOfFire",
        "pub-date" : ISODate("2013-10-02T00:00:00.000Z"),
        "rel-date" : ISODate("2013-11-02T00:00:00.000Z")
    }
    
    {
        "_id" : ObjectId("5acb662756539a6734e64e4a"),
        "title" : "WingsOfSmoke",
        "pub-date" : ISODate("2013-11-02T00:00:00.000Z"),
        "rel-date" : ISODate("2013-10-02T00:00:00.000Z")
    }
    

    ..上記のJavaコードは出力されます...

    { "_id" : { "$oid" : "5acb662756539a6734e64e4a" }, "title" : "WingsOfSmoke", "pub-date" : { "$date" : 1383350400000 }, "rel-date" : { "$date" : 1380672000000 } }
    

    ...このドキュメントのpub-date (2013-11-02T00:00:00.000Z)はrel-dateの後にあります (2013-10-02T00:00:00.000Z)。

    注:$where 演算子は機能的には同等ですが、その演算子の使用にはいくつかの制限があります



    1. すべてのフィールドが一意である場合にのみ、ドキュメントをMongoDBに挿入します

    2. MongoDB $ toBool

    3. MongoDBのクライアント側フィールドレベル暗号化の概要

    4. RailsRedisのmaxmemoryとmaxmemory-policyの設定