sql >> Database teknologi >  >> NoSQL >> MongoDB

hvordan man kører mongodb native forespørgsel med mongodb dato funktion i spring-data-mongodb?

Du kan bruge mongoTemplate.executeCommand(DBObject dbObject) variant.

Du skal bare ændre datoen til udvidet json, som understøttes af Json-parseren, og byg kommandoen.

Noget lignende

long date1 = Instant.parse("2016-07-25T10:33:04.196Z").toEpochMilli();
long date2 = Instant.parse("2018-07-25T10:33:04.196Z").toEpochMilli();

DBObject dbObject = new BasicDBObject(
    "aggregate", "mycollection").append(
    "pipeline", JSON.parse("[\n" +
        "  {\n" +
        "    \"$match\": {\n" +
        "      \"$and\": [\n" +
        "        {\n" +
        "          \"orderDate\": {\n" +
        "            \"$gte\": \""+ date1 +"\"\n" +
        "          }\n" +
        "        },\n" +
        "        {\n" +
        "          \"orderDate\": {\n" +
        "            \"$gte\": \""+ date2 +"\"\n" +
        "          }\n" +
        "        }\n" +
        "      ]\n" +
        "    }\n" +
        "  },\n" +
        "  {\n" +
        "    \"$project\": {\n" +
        "      \"orderType\": 1,\n" +
        "      \"count\": 1,\n" +
        "      \"month\": {\n" +
        "        \"$month\": [\n" +
        "          \"$orderDate\"\n" +
        "        ]\n" +
        "      }\n" +
        "    }\n" +
        "  },\n" +
        "  {\n" +
        "    \"$group\": {\n" +
        "      \"_id\": {\n" +
        "        \"month\": \"$month\",\n" +
        "        \"orderType\": \"$orderType\"\n" +
        "      },\n" +
        "      \"count\": {\n" +
        "        \"$sum\": 1\n" +
        "      }\n" +
        "    }\n" +
        "  }\n" +
    "]")).append(
    "cursor", new BasicDBObject("batchSize", 1000)
);

mongoTemplate.executeCommand(dbObject)



  1. ServerSelectionTimeoutError fejl 11001 getaddriinfo mislykkedes python

  2. arrayFilters i mongodb

  3. Mongo Query spørgsmål $gt,$lt

  4. MongoDB Atlas-godkendelse mislykkedes på Python