查询以在 mongodb 中检索数组中的多个对象 [英] query to retrieve multiple objects in an array in mongodb

查看:13
本文介绍了查询以在 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..

推荐答案

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

  1. 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 中检索数组中的多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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