通过Java驱动程序在mogodb4.0中对子文档进行排序

时间:2018-09-08 20:32:42

标签: java mongodb mongodb-query aggregation-framework

我正在尝试通过Java对子文档进行排序。我找不到所需的输出。我的数据集是:

[
{
    "_id": {
        "$oid": "5b91668a0f77e30c11574c88"
    },
    "driverId": "22",
    "busId": "55",
    "startTime": {
        "$date": 1536255626852
    },
    "location": [
        {
            "latitude": 18.5803721,
            "longitude": 73.7447051,
            "position": 0,
            "status": 1,
            "time": {
                "$date": 1536255628848
            }
        },
        {
            "latitude": 18.5803721,
            "longitude": 73.7447051,
            "position": 1,
            "status": 2,
            "time": {
                "$date": 1536255656122
            }
        },
        {
            "latitude": 18.5803721,
            "longitude": 73.7447051,
            "position": 1,
            "status": 2,
            "time": {
                "$date": 1536255656167
            }
        }
    ]
},
{
    "_id": {
        "$oid": "5b8c2cc70f77e322617c1ba1"
    },
    "driverId": "22",
    "busId": "55",
    "startTime": {
        "$date": 1535913159533
    },
    "location": [
        {
            "latitude": 18.5804663,
            "longitude": 73.7447209,
            "position": 0,
            "status": 1,
            "time": {
                "$date": 1535913160226
            }
        },
        {
            "latitude": 18.5804663,
            "longitude": 73.7447209,
            "position": 1,
            "status": 2,
            "time": {
                "$date": 1535913186460
            }
        },
        {
            "latitude": 18.5804663,
            "longitude": 73.7447209,
            "position": 1,
            "status": 2,
            "time": {
                "$date": 1535913187603
            }
        }
    ]
}
]

和我编写的代码:

AggregateIterable<Document> findIterable = tripsCollection.aggregate(Arrays.asList(
            new Document("$match", new Document("busId", busId)),
            new Document("$sort", new Document("startTime", -1)),
            new Document("$sort", new Document("location.position", -1))));

当我按_Id或startTime对文档进行排序时,将相应地输出。但是,当我尝试对子文档进行排序时,结果集不会更改。

我也尝试了其他一些变化:

Bson bsonFilterBus = Filters.eq("busId", busId);
Bson sortByDate = descending("startTime");
Bson sortByPosition = descending("location.position");
FindIterable<Document> findIterable = tripsCollection.find(bsonFilterBus).sort(sortByDate).sort(sortByPosition);

但是结果是一样的。即不基于location.position进行排序

我正在使用MongoDb 4.0。在我读过的某个地方,Mongo DB没有提供任何对子文档进行排序的方法。请帮帮我。

1 个答案:

答案 0 :(得分:1)

您必须先import React, { Component } from "react"; import Counter from "./counter"; class Counters extends Component { state = { counters: [ { id: 1, value: 5 }, { id: 2, value: 0 }, { id: 3, value: 0 }, { id: 4, value: 0 } ] }; handleIncrement = counter => { console.log(counter); }; handleReset = () => { const counters = this.state.counters.map(c => { c.value = 0; return c; }); this.setState({ counters }); }; handleDelete = counterID => { const counters = this.state.counters.filter(c => c.id !== counterID); this.setState({ counters }); }; render() { return ( <React.Fragment> <button onClick={this.handleReset} className="btn btn-dark btn-sm m-2"> Reset </button> {this.state.counters.map(counter => ( <Counter key={counter.id} onDelete={this.handleDelete} counter={counter} onIncrement={this.handleIncrement} /> ))} </React.Fragment> ); } } export default Counters; 个子文档,然后再加上$unwind,位置的位置字段和$sort,以获取位置数组。

类似

$group