auxilincom / node-mongo Goto Github PK
View Code? Open in Web Editor NEWNode Mongo โ is reactive ๐ extension to MongoDB API
License: MIT License
Node Mongo โ is reactive ๐ extension to MongoDB API
License: MIT License
https://github.com/auxilincom/node-mongo/blob/master/src/MongoService.js#L188
findOne
method returns null
if it doesn't match any documents. In that case we emit created
event with doc === null
. Why does findOneAnd
Update
function emit created
event? Maybe it is OK when options.upsert
is provided, but definitely not in the case of doc === null
.
findOneAndUpdate(query, update, options = { returnOriginal: false }) {
let originalDoc;
return this.findOne(query) // return null
.then((doc) => {
originalDoc = doc; // originalDoc === null
return this._collection.findOneAndUpdate(query, update, options); // return null
})
.then((doc) => { // doc === null
if (originalDoc) { // skipped
this._bus.emit('updated', {
doc,
prevDoc: originalDoc,
});
} else {
this._bus.emit('created', { // why?
doc, // doc === null
});
}
return doc;
});
}
With node-mongo we usually get not very helpful stack trace, like this one:
level: "error"
message: "MongoServiceError: Document not found while updating. Query: {"_id":"5cc4a6f9361f3f0012151b4c"}
at MongoService.update (/app/node_modules/@auxilin/node-mongo/src/MongoService.js:123:13)
at process._tickCallback (internal/process/next_tick.js:68:7)"
timestamp: "2019-04-29T06:38:49.924Z"
How do we improve it to show where this error has happened?
In the latest update uses updateMany internally, which is atomic and uses {$set: doc}
.
The method emits document of the following format:
{value, ok, lastErrorObject}
while actual document is expected.
https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#~findAndModifyWriteOpResult
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.