MongoDB 中的 $unwind 运算符是什么? [英] What's the $unwind operator in MongoDB?

查看:30
本文介绍了MongoDB 中的 $unwind 运算符是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用 MongoDB 的第一天,所以请放轻松 :)

This is my first day with MongoDB so please go easy with me :)

我看不懂 $unwind 操作符,可能是因为英语不是我的母语.

I can't understand the $unwind operator, maybe because English is not my native language.

db.article.aggregate(
    { $project : {
        author : 1 ,
        title : 1 ,
        tags : 1
    }},
    { $unwind : "$tags" }
);

项目操作符是我能理解的东西,我想(它就像 SELECT,不是吗?).但是,$unwind(引用)为每个源文档中的 unwound 数组的每个成员返回一个文档.

The project operator is something I can understand, I suppose (it's like SELECT, isn't it?). But then, $unwind (citing) returns one document for every member of the unwound array within every source document.

这像一个 JOIN 吗?如果是,$project (with _id, author, title and tags 字段)可以与 tags 数组进行比较吗?

Is this like a JOIN? If yes, how the result of $project (with _id, author, title and tags fields) can be compared with the tags array?

注意:我从MongoDB网站上拿的例子,我不知道tags数组的结构.我认为这是一个简单的标签名称数组.

NOTE: I've taken the example from MongoDB website, I don't know the structure of tags array. I think it's a simple array of tag names.

推荐答案

首先,欢迎来到 MongoDB!

First off, welcome to MongoDB!

要记住的是,MongoDB 采用NoSQL"方法来存储数据,因此请从您的脑海中消除选择、连接等的想法.它以文档和集合的形式存储您的数据,这允许以动态方式从您的存储位置添加和获取数据.

The thing to remember is that MongoDB employs an "NoSQL" approach to data storage, so perish the thoughts of selects, joins, etc. from your mind. The way that it stores your data is in the form of documents and collections, which allows for a dynamic means of adding and obtaining the data from your storage locations.

话虽如此,为了理解 $unwind 参数背后的概念,您首先必须了解您试图引用的用例在说什么.mongodb.org 的示例文档如下:

That being said, in order to understand the concept behind the $unwind parameter, you first must understand what the use case that you are trying to quote is saying. The example document from mongodb.org is as follows:

{
 title : "this is my title" ,
 author : "bob" ,
 posted : new Date () ,
 pageViews : 5 ,
 tags : [ "fun" , "good" , "fun" ] ,
 comments : [
             { author :"joe" , text : "this is cool" } ,
             { author :"sam" , text : "this is bad" }
 ],
 other : { foo : 5 }
}

注意标签实际上是一个包含 3 个项目的数组,在本例中是有趣"、好"和有趣".

Notice how tags is actually an array of 3 items, in this case being "fun", "good" and "fun".

$unwind 的作用是让您为每个元素剥离文档并返回生成的文档.用经典方法来考虑这一点,这相当于对于标签数组中的每个项目,返回一个仅包含该项目的文档".

What $unwind does is allow you to peel off a document for each element and returns that resulting document. To think of this in a classical approach, it would be the equivilent of "for each item in the tags array, return a document with only that item".

因此,运行结果如下:

db.article.aggregate(
    { $project : {
        author : 1 ,
        title : 1 ,
        tags : 1
    }},
    { $unwind : "$tags" }
);

将返回以下文件:

{
     "result" : [
             {
                     "_id" : ObjectId("4e6e4ef557b77501a49233f6"),
                     "title" : "this is my title",
                     "author" : "bob",
                     "tags" : "fun"
             },
             {
                     "_id" : ObjectId("4e6e4ef557b77501a49233f6"),
                     "title" : "this is my title",
                     "author" : "bob",
                     "tags" : "good"
             },
             {
                     "_id" : ObjectId("4e6e4ef557b77501a49233f6"),
                     "title" : "this is my title",
                     "author" : "bob",
                     "tags" : "fun"
             }
     ],
     "OK" : 1
}

请注意,结果数组中唯一改变的是标签值中返回的内容.如果您需要有关其工作原理的其他参考,我在此处提供了一个链接.希望这会有所帮助,并祝您进入我迄今为止遇到的最好的 NoSQL 系统之一.

Notice that the only thing changing in the result array is what is being returned in the tags value. If you need an additional reference on how this works, I've included a link here. Hopefully this helps, and good luck with your foray into one of the best NoSQL systems that I have come across thus far.

这篇关于MongoDB 中的 $unwind 运算符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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