summaryrefslogtreecommitdiff
path: root/jstests/core/update_replace.js
blob: 54f13f7ded2ac24b5491e6fe0784bb4229523749 (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
// This test checks validation of the replaced doc (on the server) for dots, $prefix and _id

// Create a new connection object so it won't affect the global connection when we modify
// it's settings.
var conn = new Mongo(db.getMongo().host);
conn.forceWriteMode(db.getMongo().writeMode());
t = conn.getDB(db.getName()).jstests_update_replace;
t.drop();

var myDB = t.getDB();
var res;

// Bypass validation in shell so we can test the server.
conn._skipValidation = true;

// Should not allow "." in field names
res = t.save({_id:1, "a.a":1});
assert(res.hasWriteError(), "a.a");

// Should not allow "." in field names, embedded
res = t.save({_id:1, a :{"a.a":1}});
assert(res.hasWriteError(), "a: a.a");

// Should not allow "$"-prefixed field names, caught before "." check
res = t.save({_id:1, $a :{"a.a":1}});
assert(res.hasWriteError(), "$a: a.a");

// Should not allow "$"-prefixed field names
res = t.save({_id:1, $a: 1});
assert(res.hasWriteError(), "$a");

// _id validation checks

// Should not allow regex _id
res = t.save({_id: /a/});
assert(res.hasWriteError(), "_id regex");

// Should not allow regex _id, even if not first
res = t.save({a:2, _id: /a/});
assert(res.hasWriteError(), "a _id regex");

// Should not allow array _id
res = t.save({_id: [9]});
assert(res.hasWriteError(), "_id array");

// This is fine since _id isn't a top level field
res = t.save({a :{ _id: [9]}});
assert(!res.hasWriteError(), "embedded _id array");

// This is fine since _id isn't a top level field
res = t.save({b:1, a :{ _id: [9]}});
assert(!res.hasWriteError(), "b embedded _id array");