summaryrefslogtreecommitdiff
path: root/jstests/core/index_partial_2dsphere.js
blob: 502f70aa55635198c918bc73f34e9ccff021a0fb (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
// A document with invalid GeoJSON should be able to be removed or updated when it is not part of
// a partial index filter.
// @tags: [requires_non_retryable_writes]

(function() {

    "use strict";

    let coll = db.index_partial_2dsphere;
    coll.drop();

    // Create a 2dsphere partial index for documents where isIndexed is greater than 0.
    let partialIndex = {geoJson: '2dsphere'};
    assert.commandWorked(
        coll.createIndex(partialIndex, {partialFilterExpression: {isIndexed: {$gt: 0}}}));

    // This document has an invalid geoJSON format (duplicated points), but will not be indexed.
    let unindexedDoc = {
        "_id": 0,
        "isIndexed": -1,
        "geoJson": {"type": "Polygon", "coordinates": [[[0, 0], [0, 1], [1, 1], [0, 1], [0, 0]]]}
    };

    // This document has valid geoJson, and will be indexed.
    let indexedDoc = {
        "_id": 1,
        "isIndexed": 1,
        "geoJson": {"type": "Polygon", "coordinates": [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]}
    };

    assert.writeOK(coll.insert(unindexedDoc));
    assert.writeOK(coll.insert(indexedDoc));

    // Return the one indexed document.
    assert.eq(1,
              coll.find({
                      isIndexed: 1,
                      geoJson: {$geoNear: {$geometry: {type: "Point", coordinates: [0, 0]}}}
                  })
                  .itcount());

    // Don't let an update to a document with an invalid geoJson succeed.
    assert.writeError(coll.update({_id: 0}, {$set: {isIndexed: 1}}));

    // Update the indexed document to remove it from the index.
    assert.writeOK(coll.update({_id: 1}, {$set: {isIndexed: -1}}));

    // This query should now return zero documents.
    assert.eq(0,
              coll.find({
                      isIndexed: 1,
                      geoJson: {$geoNear: {$geometry: {type: "Point", coordinates: [0, 0]}}}
                  })
                  .itcount());

    // Re-index the document.
    assert.writeOK(coll.update({_id: 1}, {$set: {isIndexed: 1}}));

    // Remove both should succeed without error.
    assert.writeOK(coll.remove({_id: 0}));
    assert.writeOK(coll.remove({_id: 1}));

    assert.eq(0,
              coll.find({
                      isIndexed: 1,
                      geoJson: {$geoNear: {$geometry: {type: "Point", coordinates: [0, 0]}}}
                  })
                  .itcount());
    assert.commandWorked(coll.dropIndex(partialIndex));
})();