summaryrefslogtreecommitdiff
path: root/jstests/core/geo_update_dedup.js
blob: 3191fa0df334dce3b4bf0a7c099c83fe7091be72 (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
// Cannot implicitly shard accessed collections because single updates are not targeted.
// @tags: [
//   assumes_unsharded_collection,
//   requires_non_retryable_writes,
// ]

// Test that updates with geo queries which match
// the same document multiple times only apply
// the update once

var t = db.jstests_geo_update_dedup;

// 2d index with $near
t.drop();
t.createIndex({locs: "2d"});
t.save({locs: [[49.999, 49.999], [50.0, 50.0], [50.001, 50.001]]});

var q = {locs: {$near: [50.0, 50.0]}};
assert.eq(1, t.find(q).itcount(), 'duplicates returned from query');

var res = t.update({locs: {$near: [50.0, 50.0]}}, {$inc: {touchCount: 1}}, false, true);
assert.eq(1, res.nMatched);
assert.eq(1, t.findOne().touchCount);

t.drop();
t.createIndex({locs: "2d"});
t.save({locs: [{x: 49.999, y: 49.999}, {x: 50.0, y: 50.0}, {x: 50.001, y: 50.001}]});
res = t.update({locs: {$near: {x: 50.0, y: 50.0}}}, {$inc: {touchCount: 1}});
assert.eq(1, res.nMatched);
assert.eq(1, t.findOne().touchCount);

// 2d index with $within
t.drop();
t.createIndex({loc: "2d"});
t.save({loc: [[0, 0], [1, 1]]});

res = t.update({loc: {$within: {$center: [[0, 0], 2]}}}, {$inc: {touchCount: 1}}, false, true);
assert.eq(1, res.nMatched);
assert.eq(1, t.findOne().touchCount);

// 2dsphere index with $geoNear
t.drop();
t.createIndex({geo: "2dsphere"});
var x = {
    "type": "Polygon",
    "coordinates": [[[49.999, 49.999], [50.0, 50.0], [50.001, 50.001], [49.999, 49.999]]]
};
t.save({geo: x});

res = t.update({geo: {$geoNear: {"type": "Point", "coordinates": [50.0, 50.0]}}},
               {$inc: {touchCount: 1}},
               false,
               true);
assert.eq(1, res.nMatched);
assert.eq(1, t.findOne().touchCount);

t.drop();
var locdata = [
    {geo: {type: "Point", coordinates: [49.999, 49.999]}},
    {geo: {type: "Point", coordinates: [50.000, 50.000]}},
    {geo: {type: "Point", coordinates: [50.001, 50.001]}}
];
t.save({locdata: locdata, count: 0});
t.createIndex({"locdata.geo": "2dsphere"});

res = t.update({"locdata.geo": {$geoNear: {"type": "Point", "coordinates": [50.0, 50.0]}}},
               {$inc: {touchCount: 1}},
               false,
               true);
assert.eq(1, res.nMatched);
assert.eq(1, t.findOne().touchCount);