我们可以一起编写mongodb crud查询和聚合查询吗? [英] can we write mongodb crud queries and aggregate query together?

查看:134
本文介绍了我们可以一起编写mongodb crud查询和聚合查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MongoDB中,我们可以执行以下书面查询吗?

In MongoDB can we execute below written query?

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

否则,我们还有其他方法可以一起执行CRUD和汇总查询.

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

推荐答案

简短答案-不,您不能这样做:.find(userName:"abc").aggregate([])

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

聚合管道被大量用于reads它与.find()最为相似,但是可以借助多个stages&来执行复杂的查询许多aggregation-operators.聚合只有两个阶段 $ out & ; $ merge 可以对数据库执行写操作-这些相较于其他阶段&仅在需要时才需要使用&由于它们必须是聚合流水线的最后阶段,因此必须对所有先前的阶段进行很好的测试.因此,当涉及到CRUD消除CUD时,您将受益于R-阅读.

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.

相同的.find(userName:"abc")可以写为:

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

这篇关于我们可以一起编写mongodb crud查询和聚合查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆