can we write mongodb crud queries and aggregate query together?

北城以北 提交于 2020-06-15 07:09:07

问题


In MongoDB can we execute below written query?

db.dbaname.find(userName:"abc").aggregate([])

else is there any other way we can execute CRUD and aggregate query together.


回答1:


Short answer - No you can't do this : .find(userName:"abc").aggregate([])

aggregation-pipeline is heavily used for reads which is mostly similar to .find() but capable of executing complex queries with help of it's multiple stages & many aggregation-operators. there are only two stages in aggregation $out & $merge that can perform writes to database - these stages are not that much used compared to other stages & needs to be used only when needed & as they need to be last stages in aggregation pipeline, then all the previous stages are to be tested very well. So when it comes to CRUD eliminating CUD you'll benefit over R - Reads.

Same .find(userName:"abc") can be written as :

.aggregate( [ { $match : { userName:"abc"} } ] ) // Using `$match` stage


来源:https://stackoverflow.com/questions/62108983/can-we-write-mongodb-crud-queries-and-aggregate-query-together

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