Useful new features of MongoDB 3.x see [2]--using $lookup for multi-table association processing

Source: Internet
Author: User

One of the most interesting things we've seen about MongoDB is that $lookup, we know that MongoDB is a document-based database, and it's also the most like relational database.

A nosql, however, since MongoDB is modeless, it's hard to play a role in a relational database that is very good at multi-table correlation, before which we can use Dbref, but

Yes, in MongoDB 3.2 has added a pretty good way to you, that is $lookup, and placed in the Aggreation this heavyweight pipeline analysis framework, nature is a class

A citizen, a great ha ~.

  

One: Introduction aggregate

This aggregate is a very heavyweight tool in MongoDB, and the theory of pipeline piping model is that the data source behind the operation is derived from the result of the last operation, which

It should be well understood, well, let's take a look at some of the best citizens in aggreation.

Very simple, is the above 13, for example, with the so-called $group operation, we can take this aggreation to do group processing, with $sort operation, you can arrange the results

The $out operation, we can put the results in a collections or inline mode display ...

1. $lookup

Having said so much, let's simply demonstrate that we have a product table with an Orders table, there is a foreign key relationship, and we're going to build some data.

Db.product.insert ({"_id":1,"ProductName":"Item 1"," Price": the}) Db.product.insert ({"_id":2,"ProductName":"Item 2"," Price": $}) Db.orders.insert ({"_id":1,"PID":1,"Ordername":"Order 1"}) Db.orders.insert ({"_id":2,"PID":2,"Ordername":"Order 2"}) Db.orders.insert ({"_id":3,"PID":2,"Ordername":"Order 3"}) Db.orders.insert ({"_id":4,"PID":1,"Ordername":"Order 4"}) Db.product.find () Db.orders.find ()

Well, the data is already constructed, and the next little thing we need to do is to find the order for the price of product >20 in the Orders table, which looks very simple, right,

However, there is no price field in our Orders table, so the first step is:

<1> $lookup Table Association

1 Db.product.aggregate ([2     {3 $lookup:4         {5            from:"orders",6Localfield:"_id",7Foreignfield:"PID",8            as:"Inventory_docs"9         }Ten    } One])

The results are then shown as follows:

1 /*1*/2 {3     "_id":1.0,4     "ProductName":"Item 1",5     " Price":15.0,6     "Inventory_docs" : [ 7         {8             "_id":1.0,9             "PID":1.0,Ten             "Ordername":"Order 1" One         },  A         { -             "_id":4.0, -             "PID":1.0, the             "Ordername":"Order 4" -         } -     ] - } +  - /*2*/ + { A     "_id":2.0, at     "ProductName":"Item 2", -     " Price":36.0, -     "Inventory_docs" : [  -         { -             "_id":2.0, -             "PID":2.0, in             "Ordername":"Order 2" -         },  to         { +             "_id":3.0, -             "PID":2.0, the             "Ordername":"Order 3" *         } $     ]Panax Notoginseng}

Finally I put a picture, so good seeing is true:

Let me briefly describe some of the parameters in the $lookup:

From: Table "Orders" to be associated

Localfield: the "Product" table requires an associated key.

Foreignfield: Matching key for "orders".

As: the data for the corresponding foreign key collection, "because it could be a couple of more, right?"

Well, the table association is already done, and then we need to use another keyword called the $match,where condition meaning ...

<2> $match Screening

1 Db.product.aggregate ([2     {3 $lookup:4         {5            from:"orders",6Localfield:"_id",7Foreignfield:"PID",8            as:"Inventory_docs"9         }Ten    }, One{$match: {price: {$gt: -} } } A])

As expected, we filtered out the "merchandise 1" because it was less than 20, right, but it doesn't look perfect, so I just need orders information and I don't want

The so-called product these properties, this time we can use $project to do a select operation ...

<3> $project selection Fields

Next we use $project to specify the fields I want to get, such as I just need to inventory_docs the field.

OK, when I executed, now is this hanging look, this is what this article said, is not very simple, at the same time, I also give you a simple introduction of the use of the next aggreation, is not

is very fun, of course aggregate below there are a lot of other $ operators, you can follow the requirements to try Oh ~ Remember this is pipeline mode oh ...

Useful new features of MongoDB 3.x see [2]--using $lookup for multi-table association processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.