查询以在 mongodb 中检索数组中的多个对象

编程入门 行业动态 更新时间:2024-10-22 21:38:27
本文介绍了查询以在 mongodb 中检索数组中的多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有一个对象数组,如下所示.

Suppose I have a array of objects as below.

"array" : [ { "id" : 1 }, { "id" : 2 }, { "id" : 2 }, { "id" : 4 } ]

如果我想从这个数组中检索多个对象 ({id : 2}),聚合查询是这样的.

If I want to retrieve multiple objects ({id : 2}) from this array, the aggregation query goes like this.

db.coll.aggregate([{ $match : {"_id" : ObjectId("5492690f72ae469b0e37b61c")}}, { $unwind : "$array"}, { $match : { "array.id" : 2}}, { $group : { _id : "$_id", array : { $push : { id : "$array.id"}}}} ])

以上聚合的输出为

{ "_id" : ObjectId("5492690f72ae469b0e37b61c"), "array" : [ { "id" : 2 }, { "id" : 2 } ] }

现在的问题是:1) 是否可以在 MongoDB 中使用 find() 从数组中检索多个对象?

Now the question is: 1) Is retrieving of multiple objects from an array possible using find() in MongoDB?

2) 关于性能,聚合是正确的做法吗?(因为我们需要使用四个管道运算符)?

2) With respect to performance, is aggregation is the correct way to do? (Because we need to use four pipeline operators) ?

3) 我们可以使用 Java 操作(循环数组并只保留 {id : 2} 对象)来执行此操作吗?find({"_id" : ObjectId("5492690f72ae469b0e37b61c")}) 查询?因为 find 将一次检索文档并将其保存在 RAM 中.但是如果我们使用聚合,需要在 RAM 中执行四个操作才能获得输出.

3) Can we use Java manipulation (looping the array and only keep {id : 2} objects) to do this after find({"_id" : ObjectId("5492690f72ae469b0e37b61c")}) query? Because find will once retrieve the document and keeps it in RAM. But if we use aggregation four operations need to be performed in RAM to get the output.

为什么我问 3) 问题是:假设如果有数千个客户端同时访问,那么 RAM 内存将过载.如果使用 Java 完成,RAM 上的任务更少.

Why I asked the 3) question is: Suppose if thousands of clients accessing at the same time, then RAM memory will be overloaded. If it is done using Java, less task on RAM.

4) workingSet 将在 RAM 中保留多长时间??

我的理解正确吗???

如果我错了,请纠正我.

Please correct me if I am wrong.

请建议我对此有正确的见解..

Please suggest me to have right insight on this..

推荐答案

  • 没有.你用 $ 投射第一个匹配的,你投射所有的,或者你一个都不投射.

  • No. You project the first matching one with $, you project all of them, or you project none of them.

    无所谓.如果您必须使用这个数组,聚合将允许您提取多个匹配的元素,但正确的解决方案,从概念上和性能上来说,是设计您的文档结构,这样这个问题就不会出现,或者只出现在罕见的查询中其表现不是特别重要.

    No-ish. If you have to work with this array, aggregation is what will allow you to extract multiple matching elements, but the correct solution, conceptually and for performance, is to design your document structure so this problem does not arise, or arises only for rare queries whose performance is not particularly important.

    是的.

    我们没有任何信息可以对这个问题给出合理的答案.这也超出了问题的其余部分的范围,应该是一个单独的问题.

    We have no information that would allow us to give a reasonable answer to this question. This is also out of scope relative to the rest of the question and should be a separate question.

  • 更多推荐

    查询以在 mongodb 中检索数组中的多个对象

    本文发布于:2023-11-22 19:31:24,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1618724.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:多个   组中   对象   mongodb

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!