MongoDB:查询和检索嵌入数组中的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11159912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB: Query and retrieve objects inside embedded array?
提问by nebs
Let's say I have the following document schema in a collection called 'users':
假设我在名为“users”的集合中有以下文档架构:
{
name: 'John',
items: [ {}, {}, {}, ... ]
}
The 'items' array contains objects in the following format:
'items' 数组包含以下格式的对象:
{
item_id: "1234",
name: "some item"
}
Each user can have multiple items embedded in the 'items' array.
每个用户都可以在“items”数组中嵌入多个项目。
Now, I want to be able to fetch an item by an item_id for a given user.
现在,我希望能够通过给定用户的 item_id 获取项目。
For example, I want to get the item with id "1234" that belong to the user with name "John".
例如,我想获取 ID 为“1234”的项目,该项目属于名为“John”的用户。
Can I do this with mongoDB? I'd like to utilize its powerful array indexing, but I'm not sure if you can run queries on embedded arrays and return objects from the array instead of the document that contains it.
我可以用 mongoDB 做到这一点吗?我想利用其强大的数组索引,但我不确定您是否可以对嵌入式数组运行查询并从数组而不是包含它的文档返回对象。
I know I can fetch users that have a certain item using {users.items.item_id: "1234"}. But I want to fetch the actual item from the array, not the user.
我知道我可以使用 {users.items.item_id: "1234"} 获取拥有特定项目的用户。但我想从数组中获取实际项目,而不是用户。
Alternatively, is there maybe a better way to organize this data so that I can easily get what I want? I'm still fairly new to mongodb.
或者,是否有更好的方法来组织这些数据,以便我可以轻松获得我想要的东西?我对 mongodb 还是很陌生。
Thanks for any help or advice you can provide.
感谢您提供的任何帮助或建议。
回答by yesnault
The question is old, but the response has changed since the time. With MongoDB >= 2.2, you can do :
这个问题很老,但从那时起反应已经改变了。使用 MongoDB >= 2.2,您可以执行以下操作:
db.users.find( { name: "John"}, { items: { $elemMatch: { item_id: "1234" } } })
You will have :
你将会有 :
{
name: "John",
items:
[
{
item_id: "1234",
name: "some item"
}
]
}
回答by William Z
There are a couple of things to note about this:
有几点需要注意:
1) I find that the hardest thing for folks learning MongoDB is UN-learning the relational thinking that they're used to. Your data model looks to be the right one.
1) 我发现对于学习 MongoDB 的人来说,最难的事情是不学习他们习惯的关系思维。您的数据模型看起来是正确的。
2) Normally, what you do with MongoDB is return the entire document into the client program, and then search for the portion of the document that you want on the client side using your client programming language.
2)一般情况下,你用MongoDB做的是将整个文档返回给客户端程序,然后在客户端使用你的客户端编程语言搜索你想要的文档部分。
In your example, you'd fetch the entire 'user' document and then iterate through the 'items[]' array on the client side.
在您的示例中,您将获取整个 'user' 文档,然后遍历客户端的 'items[]' 数组。
3) If you want to return just the 'items[]' array, you can do so by using the 'Field Selection' syntax. See http://www.mongodb.org/display/DOCS/Querying#Querying-FieldSelectionfor details. Unfortunately, it will return the entire 'items[]' array, and not just one element of the array.
3) 如果您只想返回 'items[]' 数组,您可以使用 'Field Selection' 语法来实现。有关详细信息,请参阅http://www.mongodb.org/display/DOCS/Querying#Querying-FieldSelection。不幸的是,它将返回整个 'items[]' 数组,而不仅仅是数组的一个元素。
4) There is an existing Jira ticket to add this functionality: it is https://jira.mongodb.org/browse/SERVER-828SERVER-828. It looks like it's been added to the latest 2.1 (development) branch: that means it will be available for production use when release 2.2 ships.
4) 有一个现有的 Jira 票来添加此功能:它是https://jira.mongodb.org/browse/SERVER-828SERVER-828。看起来它已被添加到最新的 2.1(开发)分支:这意味着它可以在 2.2 版发布时用于生产。
回答by Sergio Tulentsev
If this is an embedded array, then you can't retrieve its elements directly. The retrieved document will have form of a user (root document), although not all fields may be filled (depending on your query).
如果这是一个嵌入式数组,则无法直接检索其元素。检索到的文档将具有用户(根文档)的形式,尽管并非所有字段都可以填写(取决于您的查询)。
If you want to retrieve just that element, then you have to store it as a separate document in a separate collection. It will have one additional field, user_id
(can be part of _id
). Then it's trivial to do what you want.
如果您只想检索该元素,则必须将其作为单独的文档存储在单独的集合中。它将有一个额外的字段,user_id
(可以是 的一部分_id
)。然后做你想做的事就很简单了。
A sample document might look like this:
示例文档可能如下所示:
{
_id: {user_id: ObjectId, item_id: "1234"},
name: "some item"
}
Note that this structure ensures uniqueness of item_id
per user (I'm not sure you want this or not).
请注意,此结构可确保item_id
每个用户的唯一性(我不确定您是否想要)。