Closed naivefun closed 7 years ago
I write the upsert method like this:
upsert
public upsert(doc: Object, collectionName: string) { if (!doc) return Promise.reject('invalid doc'); doc['updatedAt'] = Date.now(); let id = doc['_id']; if (id) { return this.get(id, collectionName) .toArray((err: Error, docs: Object[]) => { if (!err && docs && docs.length === 1) { // found one, update return this.collection(collectionName).update({ _id: id }, doc); } else { // found no record, insert return this.insert(doc, collectionName); } }); } else { // missing _id, insert directly return this.insert(doc, collectionName); } }
And I call it like this:
public try() { this.db.upsert({ _id: '12345', name: '123' }, DbService.COL_MAIN) .then(insertResult => { this.db.find({}, DbService.COL_MAIN) .forEach(doc => { console.log('result:', doc); }); }) .catch((err: Error) => console.error(err, err.name, err.message)); }
The value of name 123 is not updated in the inner find and forEach. That's to say when I call find the updates are not guaranteed?
123
find
forEach
Hum false alarm ...
I write the
upsert
method like this:And I call it like this:
The value of name
123
is not updated in the innerfind
andforEach
. That's to say when I callfind
the updates are not guaranteed?