简体   繁体   English

如何更快地在 MongoDB 中获得等效的 function? WHERE 子句?

[英]How do i get this function equivalent in MongoDB, faster? WHERE clause?

i want to know how to query like MYSQL in java this function equivalent: SELECT nick FROM profiles WHERE uuid = 'something'; i want to know how to query like MYSQL in java this function equivalent: SELECT nick FROM profiles WHERE uuid = 'something';

i have done something like that in my project but it seens too slow and its returning the whole document of the profile requested, unless i use document.get("nick") i get exactly what i want, i'm sorry but i'm really new to MongoDB and i haven't already founded something about this, everything that i found it doesn't really helped me so much..我在我的项目中做过类似的事情,但它看起来太慢了,并且它返回了所请求的配置文件的整个文档,除非我使用 document.get("nick") 我得到了我想要的,对不起,但我我对 MongoDB 真的很陌生,我还没有为此建立一些东西,我发现它并没有真正帮助我太多..

this is the code:这是代码:

FindIterable<Document> results = Main.getCollection(Main.getDatabase(plugin.database), plugin.collection).find(Document.parse("{ uid: '00000000-0000-0000-0009-01fbbfda08e8' }, { nick: 1 }")); 
           Document document = results.first();
           if(document.get("nick") != player.getName()) {
               System.out.println("do something");
           }

by document var from FindIterable it returns the whole document, but i just want to return the nick of that profile, which the uuid is "00000000-0000-0000-0009-01fbbfda08e8" so i dont need to use document.get and it will be much better for performance, if thats possible.. (sorry for bad english)通过 FindIterable 中的文档 var 它返回整个文档,但我只想返回该配置文件的昵称,其 uuid 为“00000000-0000-0000-0009-01fbbfda08e8”所以我不需要使用 document.get 它会如果可能的话,性能要好得多..(抱歉英语不好)

I suggest you to take a look at how indexes and covered queries work (projections), creating an index for a specific fields would improve your performance for those common queries, and projections would help you to get only the fields you need.我建议您查看索引和覆盖查询的工作方式(投影),为特定字段创建索引将提高您对那些常见查询的性能,而投影将帮助您仅获取所需的字段。

There are plenty of examples (in java also) https://docs.mongodb.com/manual/indexes/#covered-queries有很多例子(在java中)

Here there is an example of exactly what you are asking for: https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/这里有一个您所要求的示例: https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/

Field projection applied on your example:应用于您的示例的现场投影:

.find(eq("uid", "00000000-0000-0000-0009-01fbbfda08e8")).projection(fields(include("nick"), excludeId()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Java 中的 MongoDB $where 子句中使用 lambda 函数 - Using lambda function in MongoDB $where clause in Java 如何在参数化 SQL 查询中使用多个 WHERE 子句? - How do I use multiple WHERE clause in parameterized SQL query? MongoDB相当于WHERE IN(1,2,...) - MongoDB equivalent of WHERE IN(1,2,…) 如何使我的 function 用于我的数据库的测试数据更快? - How do I make my function for testdata for my database faster? 如何将Java中的SHA函数转换为Ruby中的等效函数? - How do I translate this SHA function in Java to an equivalent in Ruby? mongodb与Java中的where子句不同 - mongodb distinct with where clause in java java到mongoDB where子句中的日期 - java to mongoDB Date in where clause 如何在where子句中使用多个条件在mongodb java中进行搜索 - how to search in mongodb java with multiple conditions in where clause Hadoop和MapReduce,如何将等效的从csv提取的线数组发送到map函数,其中每个数组都包含x-y行; - Hadoop and MapReduce, How do I send the equivalent of an array of lines pulled from a csv to the map function, where each array contained lines x - y; Amazon Cognito:从哪里可以获得initiatiateAuth函数的初始令牌? - Amazon Cognito: Where do I get the initial token for the initiatiateAuth function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM