问题
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