Mongoose debug writes to STDERR?

我们两清 提交于 2019-12-01 11:21:23

The debug option accepts a function instead of a boolean:

mongoose.set("debug", function (collection, method, paramA, paramB, paramC) {

    console.log(collection)
    console.log(method)
    console.log(paramA)
    console.log(paramB)
    console.log(paramC)
})

The reason I put paramA, paramB, paramC is because the arguments are dependent upon the method and options being used:

Person.create({firstName: "john"}, callback)
// people
// insert
// {firstName: "john"}
// undefined
// undefined

Person.update({firstName: "john"}, {lastName: "doe"}, {new: true}, callback);
// people
// update
// {firstName: "john"}
// {$set: {lastName: "doe"}}
// {new: true}

Person.find({firstName: "john"}, callback);
// people
// find
// {firstName: "john"}
// undefined
// undefined

Person.find({firstName: "john"}, {limit: 1}, callback);
// people
// find
// {firstName: "john"}
// {limit: 1}
// undefined

The info being logged is the Mongodb input, not the Mongoose input. You can see this in the update() method, paramB comes out as {$set: {lastName: "doe"}}. Behind the scenes, Mongoose converts updates to use $set, which is why that is logged.

From this, you can easily just format it however you want and process.stdout.write()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!