简体   繁体   English

MongoDB 如何查找数组中包含特定字符串的所有文档?

[英]MongoDB how to find all documents who's array contains a specific string?

Below is an example of my "Event" document in MongoDB. I want to be able to query all of the Event documents where the attendees array contains "623d03730e82c57fefa52fb2" (a user ID).下面是我在 MongoDB 中的“事件”文档的示例。我希望能够查询与会者数组包含“623d03730e82c57fefa52fb2”(用户 ID)的所有事件文档。

Here is one of my event documents:这是我的活动文件之一:

_id: ObjectId(623ce74372a28f08dea6c959)
description: "Fun BBQ to celebrate my 21st!"
host: "623d03730e82c57fefa52fb2"
invitees: Array
location: "My address..."
name: "Fun Birthday BBQ"
private: true
date: "03/28/22"
end: "11:15 PM"
start: "06:35 PM"
attendees:Array
   0: "623d03730e82c57fefa52fb2"

Here is my broken query code:这是我损坏的查询代码:

    String id = "623d03730e82c57fefa52fb2";
    // I have also tried Document queryFilter = new Document("attendees", id);
    Document queryFilter = new Document("attendees", new Document("$in", Arrays.asList(id)));

The above code always returns an empty result.上面的代码总是返回一个空结果。 To clarify I am using Java and MongoDB Realms but that shouldn't matter.澄清一下,我使用的是 Java 和 MongoDB 领域,但这无关紧要。

You don't need $in , use only $eq is ok.你不需要$in ,只使用$eq就可以了。

db.collection.find({
  attendees: "623d03730e82c57fefa52fb2"
})

mongoplayground mongo游乐场

For easier and more efficient queries, it's important that the types of field values remain consistent.为了更轻松、更高效的查询,字段值的类型保持一致很重要。

For example, if "_id" is stored at an ObjectId , then query parameters should also be of type ObjectId .例如,如果"_id"存储在ObjectId中,则查询参数也应为ObjectId类型。 Likewise, if they are strings, then consistently use strings.同样,如果它们是字符串,则始终使用字符串。

If different value types are stored for individual field values, successful queries can still be possible, but not as efficiently since the field types must be considered in the queries.如果为各个字段值存储不同的值类型,仍然可以成功查询,但效率不高,因为必须在查询中考虑字段类型。 For example, if trying to find a doc by a field that may have a string or an ObjectId type, the query must either search for both types, or the query writer must know the type beforehand.例如,如果尝试通过可能具有字符串或ObjectId类型的字段find文档,则查询必须搜索这两种类型,或者查询编写者必须事先知道类型。 It's easier and more efficient to just pick one type for a field and stick to it.只为一个字段选择一种类型并坚持下去会更容易、更有效。

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

相关问题 使用java查找包含mongodb中特定值的数组的文档 - Find documents with array that contains a specific value in mongodb using java 如何查找其数组字段仅包含两个特定值的所有文档 - How to find all documents which their array field contains only two specific values 如何使用RestHighLevelClient查找具有特定字段的文档包含字符串 - How to find documents with specific field contains string using RestHighLevelClient 如何找到两个LocalDateTime之间插入的所有MongoDB文档 - How find all MongoDB documents inserted between two LocalDateTime 如何使用 (.contains) 从数组列表中查找特定字符串 - How do I find a specific string from an array list using (.contains) 如何查找字符串是否包含数字,后跟特定字符串 - how to find if a string contains numbers followed by a specific string 如何制作正则表达式以查找字符串是否包含占位符(如%1 $ s) - How to make a regex to find if a string contains a placeholder like %1$s 如何将所有文档添加到mongoDB中的ArrayList中? - How to add all documents to an ArrayList in mongoDB? 如何在java中删除mongodb集合中的所有文档 - How to delete all documents in mongodb collection in java 如何在 MongoDB Java 中检索/查找嵌套数组的所有元素 - How to retrieve / find all elements of a nested array in MongoDB Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM