MongoDB implementation Join

Source: Internet
Author: User

MongoDB provides methods for ref and populate, and supports SQL operations like join. This article gives a practical example:

1. Data 1:

var New Schema ({    User: {        type:string    },    title: {        type:string    },    tag: [{        type: String    }],    content: {        type:string    },    count: {        type:number,        ' default ': 0     }});

2. Data 2:

var New Schema ({    name: {        type:string    }, Time    : {        type:date    },    conments: [{        // Only collection's _id        ' Daob '    }) can be used in the Id,mongo of the DAOB saved here;

3. Insert operation:

varb =NewDaobmodel ({User:' Fredric ', Title:' Title1 ', tag: [' Tag1 ', ' tag2 ', ' tag3 '], content:' Nothing to say, just test ',    }); B.save (function(err) {varA =NewDaoamodel ({name:' Sinny ', Time:NewDate (), conments: [b._id]}); A.save (function(err) {returncallback (ERR); })    });

4. Find operations:

MONGO does not support true join operations, so the requirements in this example can only be split into two steps

//You can use aggregations to implement more complex queries.    Daobmodel.find ({        ' tag ': {            $in: [' Tag1 ']        }    }, {        1      function(err, IDS) {        daoamodel.find ({            conments: {                $ in: IDS            }        ). Populate (' conments '). EXEC (function(err, Docs) {            return  Callback (err, docs);        }     );

MongoDB implementation Join

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.