-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (54 loc) · 1.52 KB
/
index.js
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
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/myapp');
const bankAccountSchema = new mongoose.Schema({
iban : String,
bic : String
});
const contactPersonSchema = new mongoose.Schema({
firstName : String,
lastName : String,
bankAccount : bankAccountSchema
});
const companySchema = new mongoose.Schema({
_id : String,
name : String,
contactPerson : contactPersonSchema,
_documentVersion : Number
}, {
timestamps : true
});
/**
* This is the issue: If companySchema is used instead of companySchema.clone(), updateMany works fine.
*/
const CompanyModel = mongoose.model('Company', companySchema.clone());
(async function () {
await CompanyModel.create({
_id : 'abcde',
name : 'ACME',
_documentVersion : 1,
});
await CompanyModel.updateMany({
"_documentVersion" : 1,
"$and" : [
{
"_id" : "abcde"
}
]
}, {
name : 'Other Company name',
contactPerson : {
firstName : 'John',
lastName : 'Doe',
bankAccount : {
iban : 'abc',
bic : 'def'
}
}
}, {
"multi" : false,
"runValidators" : true,
"context" : "query"
})
.lean(true)
.exec();
})();