summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/document_source_geo_near.cpp
blob: 7c2476985e86989bcd5abaf5a842b96224e34867 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
 * Copyright 2011 (c) 10gen Inc.
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mongo/pch.h"

#include "mongo/db/pipeline/document_source.h"
#include "mongo/db/pipeline/document.h"

namespace mongo {
    char DocumentSourceGeoNear::geoNearName[] = "$geoNear";
    const char *DocumentSourceGeoNear::getSourceName() const { return geoNearName; }

    boost::optional<Document> DocumentSourceGeoNear::getNext() {
        pExpCtx->checkForInterrupt();

        if (!resultsIterator)
            runCommand();

        if (!resultsIterator->more())
            return boost::none;

        // each result from the geoNear command is wrapped in a wrapper object with "obj",
        // "dis" and maybe "loc" fields. We want to take the object from "obj" and inject the
        // other fields into it.
        Document result (resultsIterator->next().embeddedObject());
        MutableDocument output (result["obj"].getDocument());
        output.setNestedField(*distanceField, result["dis"]);
        if (includeLocs)
            output.setNestedField(*includeLocs, result["loc"]);

        return output.freeze();
    }

    void DocumentSourceGeoNear::setSource(DocumentSource*) {
        uasserted(16602, "$geoNear is only allowed as the first pipeline stage");
    }

    bool DocumentSourceGeoNear::coalesce(const intrusive_ptr<DocumentSource> &pNextSource) {
        DocumentSourceLimit* limitSrc = dynamic_cast<DocumentSourceLimit*>(pNextSource.get());
        if (limitSrc) {
            limit = min(limit, limitSrc->getLimit());
            return true;
        }

        return false;
    }

    // This command is sent as-is to the shards.
    // On router this becomes a sort by distance (nearest-first) with limit.
    intrusive_ptr<DocumentSource> DocumentSourceGeoNear::getShardSource() { return this; }
    intrusive_ptr<DocumentSource> DocumentSourceGeoNear::getRouterSource() {
        return DocumentSourceSort::create(pExpCtx,
                                          BSON(distanceField->getPath(false) << 1),
                                          limit);
    }

    Value DocumentSourceGeoNear::serialize(bool explain) const {
        MutableDocument result;

        if (coordsIsArray) {
            result.setField("near", Value(BSONArray(coords)));
        }
        else {
            result.setField("near", Value(coords));
        }

        // not in buildGeoNearCmd
        result.setField("distanceField", Value(distanceField->getPath(false)));

        result.setField("limit", Value(limit));

        if (maxDistance > 0)
            result.setField("maxDistance", Value(maxDistance));

        result.setField("query", Value(query));
        result.setField("spherical", Value(spherical));
        result.setField("distanceMultiplier", Value(distanceMultiplier));

        if (includeLocs)
            result.setField("includeLocs", Value(includeLocs->getPath(false)));

        result.setField("uniqueDocs", Value(uniqueDocs));

        return Value(DOC(getSourceName() << result.freeze()));
    }

    BSONObj DocumentSourceGeoNear::buildGeoNearCmd() const {
        // this is very similar to sourceToBson, but slightly different.
        // differences will be noted.

        BSONObjBuilder geoNear; // not building a subField

        geoNear.append("geoNear", pExpCtx->ns.coll()); // not in toBson

        if (coordsIsArray) {
            geoNear.appendArray("near", coords);
        }
        else {
            geoNear.append("near", coords);
        }

        geoNear.append("num", limit); // called limit in toBson

        if (maxDistance > 0)
            geoNear.append("maxDistance", maxDistance);

        geoNear.append("query", query);
        geoNear.append("spherical", spherical);
        geoNear.append("distanceMultiplier", distanceMultiplier);

        if (includeLocs)
            geoNear.append("includeLocs", true); // String in toBson

        geoNear.append("uniqueDocs", uniqueDocs);

        return geoNear.obj();
    }

    void DocumentSourceGeoNear::runCommand() {
        massert(16603, "Already ran geoNearCommand",
                !resultsIterator);

        bool ok = _mongod->directClient()->runCommand(pExpCtx->ns.db().toString(),
                                                      buildGeoNearCmd(),
                                                      cmdOutput);
        uassert(16604, "geoNear command failed: " + cmdOutput.toString(),
                ok);

        resultsIterator.reset(new BSONObjIterator(cmdOutput["results"].embeddedObject()));
    }

    intrusive_ptr<DocumentSourceGeoNear> DocumentSourceGeoNear::create(
            const intrusive_ptr<ExpressionContext> &pCtx) {
        return new DocumentSourceGeoNear(pCtx);
    }

    intrusive_ptr<DocumentSource> DocumentSourceGeoNear::createFromBson(
            BSONElement *pBsonElement,
            const intrusive_ptr<ExpressionContext> &pCtx) {
        intrusive_ptr<DocumentSourceGeoNear> out = new DocumentSourceGeoNear(pCtx);
        out->parseOptions(pBsonElement->embeddedObjectUserCheck());
        return out;
    }

    void DocumentSourceGeoNear::parseOptions(BSONObj options) {
        // near and distanceField are required

        uassert(16605, "$geoNear requires a 'near' option as an Array",
                options["near"].isABSONObj()); // Array or Object (Object is deprecated)
        coordsIsArray = options["near"].type() == Array;
        coords = options["near"].embeddedObject().getOwned();

        uassert(16606, "$geoNear requires a 'distanceField' option as a String",
                options["distanceField"].type() == String);
        distanceField.reset(new FieldPath(options["distanceField"].str()));

        // remaining fields are optional

        // num and limit are synonyms
        if (options["limit"].isNumber())
            limit = options["limit"].numberLong();
        if (options["num"].isNumber())
            limit = options["num"].numberLong();

        if (options["maxDistance"].isNumber())
            maxDistance = options["maxDistance"].numberDouble();

        if (options["query"].type() == Object)
            query = options["query"].embeddedObject().getOwned();

        spherical = options["spherical"].trueValue();

        if (options["distanceMultiplier"].isNumber())
            distanceMultiplier = options["distanceMultiplier"].numberDouble();

        if (options.hasField("includeLocs")) {
            uassert(16607, "$geoNear requires that 'includeLocs' option is a String",
                    options["includeLocs"].type() == String);
            includeLocs.reset(new FieldPath(options["includeLocs"].str()));
        }

        uniqueDocs = options["uniqueDocs"].trueValue();
    }

    DocumentSourceGeoNear::DocumentSourceGeoNear(const intrusive_ptr<ExpressionContext> &pExpCtx)
        : SplittableDocumentSource(pExpCtx)
        , coordsIsArray(false)
        , limit(100)
        , maxDistance(-1.0)
        , spherical(false)
        , distanceMultiplier(1.0)
        , uniqueDocs(true)
    {}
}